Skip to content

Commit

Permalink
Merge branch 'key_system'
Browse files Browse the repository at this point in the history
  • Loading branch information
b1021204 committed Oct 31, 2024
2 parents 50c48ad + 0e48521 commit e92aa9f
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 98 deletions.
27 changes: 0 additions & 27 deletions funawskeyb1021204.pem

This file was deleted.

183 changes: 173 additions & 10 deletions internal/provider/VM_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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アドレスをスクレイピングする関数
Expand All @@ -57,6 +60,9 @@ type ip struct{}
// VMのパスワードをスクレイピングする関数
type machine_pass struct{}

// VM の鍵をダウンロードし、アドレスを返す関数
type key struct{}

func NewIp() function.Function {
return &ip{}
}
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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,
},
},
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Check failure on line 637 in internal/provider/VM_resource.go

View workflow job for this annotation

GitHub Actions / Build

Error return value of `driver.Stop` is not checked (errcheck)
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)

Check failure on line 655 in internal/provider/VM_resource.go

View workflow job for this annotation

GitHub Actions / Build

Error return value of `elem_user.Fill` is not checked (errcheck)
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
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func (p *scrapingProvider) Functions(_ context.Context) []func() function.Functi
return []func() function.Function{
NewIp,
NewMachinePass,
NewKey,
}

}
21 changes: 21 additions & 0 deletions internal/provider/start_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -40,5 +42,7 @@ output "ip" {
value = provider::scraping::ip("b1021204", "SAKURAskip108", "Linux(Ubuntu22.04LTS)(2024前期)(10/31廃止)", "EC2-geotail-146000")
}
*/


}
6 changes: 0 additions & 6 deletions terraform-registry-manifest.json

This file was deleted.

41 changes: 32 additions & 9 deletions terraform.tfstate
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit e92aa9f

Please sign in to comment.