diff --git a/funawskeyb1021204.pem b/funawskeyb1021204.pem deleted file mode 100644 index ab4bd23..0000000 --- a/funawskeyb1021204.pem +++ /dev/null @@ -1,27 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -MIIEpAIBAAKCAQEAuJrIDJmxuEH8W4p5sn+FK4Mt3bDfe73QkDSZ5FPbw0DICdBy -+it9sbaerWFjKmdEKnBAxo5OJhlNNn2XJm4nP2b3cMpc24wcgQnJnxqizt1gdTsX -prUP8Vh3MC+Emf+T7eGMBXsRCNHtAdOIN14uEWUu9tkRaR6o2Z7C5WZ+6Mb+ky98 -eEY3pr8jOznFo55LmlKUFfifDBDWzOaH1YGtjOoj+HeVspZGwB/rfWWrLuSKm3jc -/8pt8WLhwE437bhi2dQ1cWiWWxveoZOvR3quOiNON4AzVKLwt58TZMRSNpcPNXUN -KFWEEhDVJo4+4+DXQDVCvFjv5lJE4cXARrnvLwIDAQABAoIBAQCrsIjN70OysuwZ -1zhIIwGf7mkB/jZbx7Xp6IIYMUX62o0xU4Ub1scYOVhquGCaV2C2d6BHCqL0HCQL -wdKKE3i9Ju6LICospqr7+DZkm0lAS/QiE8tzaIJjXHu9fPQ+pczXOSYZEtRM97EO -OcY+eIEv/IMpYcknkzRG8bkh0anGo3RO6bZIA1LjwS0OqBVejimK/qegXitOJ3ml -XPDAnL/X9VqmEU7zLBPY2CqPlwBnV2mEoAN1fyL8EYrgx8EQ5o2KdmhPi7dDIaAV -VXR4SLFRRnZam4hs7FsYRHKn7J3mdJu1diFtHXsWGO5FNuSnopFAdarkGCNUs0Pt -xjFaXFvBAoGBAPpBHv9GYdL49zQOgHLsVezYuKJYaq3yQwgt4EPsPW+facBgdiAm -b4YhIc8nC2AdickW65MTsD8JCv142YoCXF9Wjv2ktpiRhkzfMmOR25OJONDDhQaB -XgrF86oIlN8f9CoGvW7F1DtqM/HBFVF4WgXJNkKwxgjOx3W5D0jT2mP1AoGBALzX -zTMZZA/XmJ8sXcd7fjsaAK7HgmbIv6DwLMduxUxjwJ8nDwvSh/ZCiqVkwrkDv0i9 -3hA1I2M+Rg3vAkvnoaPQqy7WQSIdNiGbLdAJXXnwfQ5D9WIUZl0r98XGokVHR2SN -XH/lULJyn4yumSz/cCJlIBUSUJQLp1VJZZg04/QTAoGBAPB3rs9g1BMjhHaTEL5d -dlt2DESDbU7kIaE0Y/EgihA+QCHJDvjptfRO1ev1+qpK9+EwkwDqxEpt6A+UFoTU -q+o4hIIZyhNkkoY/Wm3u8+0ayGth1OthV/pGKFNUl6inTbksbM6KbdsBvBQrks6M -NQSmSetfbYt9FpB6WMOEPrAZAoGABQFPPgB5R3Y2ZFmfrq7WPKjGxdsh2KmPcquK -9Eo6TnkCXPWDD5Qn6F4BILsX8iFoS3xEy3ctFe9LmgjwraRZA2d/cgFZvpa5nZBo -WXA2GwpFtkAU2+GgiO2mZoaKPH3UJZUdk4I3blNdaTQjeS7YAQFfJU/T9OiOKdOF -LtCQDiMCgYBJAajrQsBuKE4uHQmRkvXMSEo7TmCuQmPCSwmSfVW1cJNUj2Idl5re -VGGZPvbY27ZSMJMJ3pNqj6HtMFWBAPCwj96VtiK3gG18NDSw1TgTdK8Vf97sM7YD -xpj7Qp2ffZvzJvTlwjXtAEUsZQ+WZyiz4t0lv37n93N6RfJvASt8/Q== ------END RSA PRIVATE KEY----- \ No newline at end of file diff --git a/internal/provider/VM_resource.go b/internal/provider/VM_resource.go index 538efea..a255fbe 100644 --- a/internal/provider/VM_resource.go +++ b/internal/provider/VM_resource.go @@ -23,6 +23,7 @@ var _ resource.Resource = &VMResource{} var _ resource.ResourceWithImportState = &VMResource{} var _ function.Function = &ip{} var _ function.Function = &machine_pass{} +var _ function.Function = &key{} func NewVMResource() resource.Resource { return &VMResource{} @@ -35,20 +36,22 @@ type VMResource struct { // 各関数内で使われるデータの構造体 type Machine_Data struct { - environment string - username string - password string - machine_name string - machine_stop bool + environment string + username string + password string + machine_name string + machine_stop bool + instance_type string } // ExampleResourceModel describes the resource data model. type VMResourceModel struct { - Environment types.String `tfsdk:"environment"` - Username types.String `tfsdk:"username"` - Password types.String `tfsdk:"password"` - Machine_name types.String `tfsdk:"machine_name"` - Machine_stop types.Bool `tfsdk:"machine_stop"` + Environment types.String `tfsdk:"environment"` + Username types.String `tfsdk:"username"` + Password types.String `tfsdk:"password"` + Machine_name types.String `tfsdk:"machine_name"` + Machine_stop types.Bool `tfsdk:"machine_stop"` + Instance_Type types.String `tfsdk:"instance_type"` } //  IPアドレスをスクレイピングする関数 @@ -57,6 +60,9 @@ type ip struct{} // VMのパスワードをスクレイピングする関数 type machine_pass struct{} +// VM の鍵をダウンロードし、アドレスを返す関数 +type key struct{} + func NewIp() function.Function { return &ip{} } @@ -65,6 +71,10 @@ func NewMachinePass() function.Function { return &machine_pass{} } +func NewKey() function.Function { + return &key{} +} + // ipアドレススクレイピング用のメタデータ func (f *ip) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) { resp.Name = "ip" @@ -129,6 +139,38 @@ func (f *machine_pass) Definition(ctx context.Context, req function.DefinitionRe } } +// 鍵ダウンロード関数用のメタデータ +func (f *key) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) { + resp.Name = "key" +} + +// 鍵ダウンロード関数用の定義 +func (f *key) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) { + resp.Definition = function.Definition{ + Summary: "Download ssh key and return key address", + Description: "Pleace give usenamae, pass, env and address you want to download.", + Parameters: []function.Parameter{ + function.StringParameter{ + Name: "username", + Description: "username", + }, + function.StringParameter{ + Name: "password", + Description: "pass", + }, + function.StringParameter{ + Name: "environment", + Description: "env of VM", + }, + function.StringParameter{ + Name: "address", + Description: "address which you want to download.", + }, + }, + Return: function.StringReturn{}, + } +} + // resource用のメタデータ func (r *VMResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_resource" @@ -159,6 +201,9 @@ func (r *VMResource) Schema(ctx context.Context, req resource.SchemaRequest, res "machine_stop": schema.BoolAttribute{ Optional: true, }, + "instance_type": schema.StringAttribute{ + Optional: true, + }, }, } } @@ -201,6 +246,7 @@ func (r *VMResource) Create(ctx context.Context, req resource.CreateRequest, res Machine_Data.machine_name = data.Machine_name.ValueString() Machine_Data.machine_stop = data.Machine_stop.ValueBool() Machine_Data.environment = data.Environment.ValueString() + Machine_Data.instance_type = data.Instance_Type.ValueString() ctx = tflog.SetField(ctx, "username", Machine_Data.username) ctx = tflog.SetField(ctx, "password", Machine_Data.password) @@ -247,6 +293,7 @@ func (r *VMResource) Update(ctx context.Context, req resource.UpdateRequest, res Machine_Data.environment = data.Environment.ValueString() Machine_Data.machine_name = data.Machine_name.ValueString() Machine_Data.machine_stop = data.Machine_stop.ValueBool() + Machine_Data.instance_type = data.Instance_Type.ValueString() ctx = tflog.SetField(ctx, "username", Machine_Data.username) ctx = tflog.SetField(ctx, "password", Machine_Data.password) @@ -543,3 +590,119 @@ func (f *machine_pass) Run(ctx context.Context, req function.RunRequest, resp *f resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, machine_pass)) return } + +// 鍵ダウンロード用のrun + +func (f *key) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + //var Machine_Data Machine_Data + var username string + var password string + var environment string + var address string + + // Read Terraform argument data into the variables + resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &username, &password, &environment, &address)) + driver := agouti.ChromeDriver( + // ここでChromeOptions + agouti.ChromeOptions("prefs", map[string]interface{}{ + "download.default_directory": address, + "download.prompt_for_download": false, + "download.directory_upgrade": true, + "plugins.plugins_disabled": "Chrome PDF Viewer", + "plugins.always_open_pdf_externally": true, + }), /* + agouti.ChromeOptions("args", []string{ + "--disable-extensions", + "--disable-print-preview", + "--ignore-certificate-errors", + }),*/ + agouti.Debug, + ) + /* + デバック中のためコメントアウト + driver := agouti.ChromeDriver( + agouti.ChromeOptions( + "args", []string{ + "--headless", + "--disavle-gpu", + }), + ) + */ + log.Printf("Open Google Chorome...\n") + + if err := driver.Start(); err != nil { + log.Fatalf("Failed to start driver:%v\n", err) + } + + defer driver.Stop() + page, err := driver.NewPage() + if err != nil { + log.Fatalf("Failed to open Chorome page:%v\n", err) + } + log.Printf("Success to open Google Chorome.\n") + + // access to FUN login page.. + log.Printf("Access to FUN VM WebAPI...\n") + if err := page.Navigate("https://manage.p.fun.ac.jp/server_manage"); err != nil { + log.Fatalf("Failed to access to FUN VM WebAPI:%v\n", err) + } + + time.Sleep(1 * time.Second) + + // 入力ボックスにユーザ名・パスを打ち込む + elem_user := page.FindByName("username") + elem_pass := page.FindByName("password") + elem_user.Fill(username) + elem_pass.Fill(password) + log.Printf("fill username: %v\n", username) + log.Printf("fill password\n") + + // Submit + if err := page.FindByClass("credentials_input_submit").Click(); err != nil { + log.Fatalf("Failed to login:%v\n", err) + return + } + log.Printf("Success to login FUN VM WebAPI!!\n") + + time.Sleep(1 * time.Second) + + // 環境画面の項目数を入れる関数。暫定5個に設定しておく + max_environment := 5 + for i := 1; i <= max_environment; i++ { + + log.Printf("Serch for environment: %v\n...", environment) + text, _ := page.FindByXPath("/html/body/div/div/main/div/form/div[1]/div/select/option[" + strconv.Itoa(i) + "]").Text() + if text == environment { + + log.Printf("get environment: %v\n", text) + if err := page.FindByXPath("/html/body/div/div/main/div/form/div[1]/div/select/option[" + strconv.Itoa(i) + "]").Click(); err != nil { + log.Fatalf("Failed to click environment: %v\n", err) + } + break + } + // max_environment個分のの項目をチェックしてなかった場合エラーにする + if i == max_environment { + log.Fatalf("Can't look up environment: %v\n", environment) + } + } + + // 次のページへ行く + if err := page.FindByXPath("/html/body/div/div/main/div/form/div[2]/div/span").Click(); err != nil { + log.Fatalf("faild to click next page bottuon") + } + + time.Sleep(1 * time.Second) + + //ダウンロードボタンクリック + if err := page.FindByXPath("/html/body/form/div/div[4]/div[1]/div[3]/div/div[2]/div[2]/div/a").Click(); err != nil { + log.Fatalf("Failed to Download key:%v\n", err) + return + } + + key_name, _ := page.FindByXPath("/html/body/form/div/div[4]/div[1]/div[3]/div/div[2]/div[2]/div/span").Text() + log.Printf("key name is %v and address is %v/%v.pem\n", key_name, address, key_name) + address = address + "/" + key_name + ".pem" + page.CloseWindow() + resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, address)) + return +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 1393248..03c4df1 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -115,6 +115,7 @@ func (p *scrapingProvider) Functions(_ context.Context) []func() function.Functi return []func() function.Function{ NewIp, NewMachinePass, + NewKey, } } diff --git a/internal/provider/start_vm.go b/internal/provider/start_vm.go index a14ca32..34159da 100644 --- a/internal/provider/start_vm.go +++ b/internal/provider/start_vm.go @@ -96,7 +96,28 @@ func start_vm(Machine_Data Machine_Data) { log.Printf("found machin_name = %v!!!", Machine_Data.machine_name) log.Printf("start %v...\n", Machine_Data.machine_name) + // インスタンスネームが指定されているときに、スクレイピングする + + for j := 0; j <= 4; j++ { + log.Printf("serch for instance_type = %v \n", Machine_Data.instance_type) + if Machine_Data.instance_type != "" { + instance, _ := page.FindByXPath("/html/body/form/div/div[4]/div[2]/div[" + strconv.Itoa(i+1) + "]/table/tbody/tr[1]/td[2]/div/select/option[" + strconv.Itoa(j) + "]").Text() + // a, _ := page.FindByXPath("/html/body/form/div/div[4]/div[2]/div[3]/table/tbody/tr[1]/td[2]/div/select/option[2]").Text() + log.Printf("now, scraping...: %v", instance) + if instance == Machine_Data.instance_type { + if err := page.FindByXPath("/html/body/form/div/div[4]/div[2]/div[" + strconv.Itoa(i+1) + "]/table/tbody/tr[1]/td[2]/div/select/option[" + strconv.Itoa(j) + "]").Click(); err != nil { + log.Printf("Can't choice instance_type: %v\n", Machine_Data.instance_type) + log.Fatalf("Pleace choeck instance_type\n") + return + } + + } + } + + } + // 見つけたマシン名のスタートボタンをおす + time.Sleep(1 * time.Second) if err := page.FindByName("startBtn_" + strconv.Itoa(i)).Click(); err != nil { log.Fatalf("Failed to start;%v\n", err) return diff --git a/main.tf b/main.tf index 3354cd9..17002d8 100644 --- a/main.tf +++ b/main.tf @@ -15,16 +15,18 @@ resource "scraping_resource" "example"{ environment = "Linux(Ubuntu22.04LTS)(2024前期)(10/31廃止)" username = "b1021204" password = "SAKURAskip108" - machine_name = "EC2-geotail-146000" + machine_name = "EC2-geotail-155163" machine_stop = false + instance_type = "t4g.large" +/* connection { type = "ssh" user = "ubuntu" - password = provider::scraping::ip("b1021204", "SAKURAskip108", "Linux(Ubuntu22.04LTS)(2024前期)(10/31廃止)", "EC2-geotail-146000") - private_key = file("/Users/nsysk_0101/univ/b4/terraform-provider-scraping/funawskeyb1021204.pem") - host = provider::scraping::ip("b1021204", "SAKURAskip108", "Linux(Ubuntu22.04LTS)(2024前期)(10/31廃止)", "EC2-geotail-146000") + password = provider::scraping::ip("b1021204", "SAKURAskip108", "Linux(Ubuntu22.04LTS)(2024前期)(10/31廃止)", "EC2-geotail-146055") + private_key = file(provider::scraping::key("b1021204", "SAKURAskip108", "Linux(Ubuntu22.04LTS)(2024前期)(10/31廃止)", "/Users/nsysk_0101/univ/b4/terraform-provider-scraping")) + host = provider::scraping::ip("b1021204", "SAKURAskip108", "Linux(Ubuntu22.04LTS)(2024前期)(10/31廃止)", "EC2-geotail-146055") } provisioner "remote-exec" { @@ -40,5 +42,7 @@ output "ip" { value = provider::scraping::ip("b1021204", "SAKURAskip108", "Linux(Ubuntu22.04LTS)(2024前期)(10/31廃止)", "EC2-geotail-146000") } +*/ +} \ No newline at end of file diff --git a/terraform-registry-manifest.json b/terraform-registry-manifest.json deleted file mode 100644 index fec2a56..0000000 --- a/terraform-registry-manifest.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "version": 1, - "metadata": { - "protocol_versions": ["6.0"] - } -} diff --git a/terraform.tfstate b/terraform.tfstate index f0e8b47..1d47fba 100644 --- a/terraform.tfstate +++ b/terraform.tfstate @@ -1,14 +1,37 @@ { "version": 4, - "terraform_version": "1.8.4", -<<<<<<< HEAD - "serial": 11, - "lineage": "198ae01d-118b-1b8c-d70e-da3bd819bff3", -======= - "serial": 3, - "lineage": "7f513c7d-da56-1ac1-f792-c90b484cf64d", ->>>>>>> ip_serch + "terraform_version": "1.9.7", + "serial": 1, + "lineage": "eee271dc-cea1-b954-4492-d7888391a4fb", "outputs": {}, - "resources": [], + "resources": [ + { + "mode": "managed", + "type": "scraping_resource", + "name": "example", + "provider": "provider[\"registry.terraform.io/hashicorp/scraping\"]", + "instances": [ + { + "schema_version": 0, + "attributes": { + "environment": "Linux(Ubuntu22.04LTS)(2024前期)(10/31廃止)", + "instance_type": "t4g.large", + "machine_name": "EC2-geotail-155163", + "machine_stop": false, + "password": "SAKURAskip108", + "username": "b1021204" + }, + "sensitive_attributes": [ + [ + { + "type": "get_attr", + "value": "password" + } + ] + ] + } + ] + } + ], "check_results": null } diff --git a/terraform.tfstate.backup b/terraform.tfstate.backup index 7fb9a86..5eb31ab 100644 --- a/terraform.tfstate.backup +++ b/terraform.tfstate.backup @@ -1,47 +1,9 @@ { "version": 4, - "terraform_version": "1.8.4", -<<<<<<< HEAD - "serial": 9, - "lineage": "198ae01d-118b-1b8c-d70e-da3bd819bff3", - "outputs": {}, -======= + "terraform_version": "1.9.7", "serial": 1, - "lineage": "7f513c7d-da56-1ac1-f792-c90b484cf64d", - "outputs": { - "ip": { - "value": "10.224.146.0", - "type": "string" - } - }, ->>>>>>> ip_serch - "resources": [ - { - "mode": "managed", - "type": "scraping_resource", - "name": "example", - "provider": "provider[\"registry.terraform.io/hashicorp/scraping\"]", - "instances": [ - { - "schema_version": 0, - "attributes": { - "environment": "Linux(Ubuntu22.04LTS)(2024前期)(10/31廃止)", - "machine_name": "EC2-geotail-146000", - "machine_stop": false, - "password": "SAKURAskip108", - "username": "b1021204" - }, - "sensitive_attributes": [ - [ - { - "type": "get_attr", - "value": "password" - } - ] - ] - } - ] - } - ], + "lineage": "8151eac3-2250-2af5-af4e-f9ed67767aaa", + "outputs": {}, + "resources": [], "check_results": null }