Skip to content

Commit 709db39

Browse files
author
Naoki Kanazawa
committed
contest commandを追加下
1 parent 6668a48 commit 709db39

File tree

6 files changed

+155
-1
lines changed

6 files changed

+155
-1
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,22 @@
1-
# netcon-cli
1+
# netcon-cli
2+
3+
## contestの初期化
4+
5+
```bash
6+
netcon contest init --vmms-credential ${CREDENTIAL} --mapping-file-path ./mapping.yaml --count 1
7+
```
8+
9+
## score serve
10+
11+
```bash
12+
netcon scoreserver instance get --name image-sc0-xxxxx
13+
14+
netcon scoreserver instance list
15+
```
16+
17+
## vm-management-server
18+
19+
```bash
20+
netcon vmms instance create --credential ${CREDENTIAL} --problem-id 564c4898-c55c-460f-ad0a-eab5a539514f --machine-image-name image-sc0
21+
netcon vmms instance delete --credential ${CREDENTIAL} --instance-name image-sc0-rxfe9
22+
```

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ require (
88
github.com/sacloud/libsacloud/v2 v2.11.0
99
github.com/spf13/cobra v1.1.1
1010
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
11+
gopkg.in/yaml.v2 v2.2.8
1112
)

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl
550550
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
551551
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
552552
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
553+
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
553554
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
554555
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
555556
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

mapping.example.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
- problem_id: UUID
2+
machine-image-id: image-aki
3+
- problem_id: UUID
4+
machine-image-id: image-kit
5+
- problem_id: UUID
6+
machine-image-id: image-kny
7+
- problem_id: UUID
8+
machine-image-id: image-nas
9+
- problem_id: UUID
10+
machine-image-id: image-pea
11+
- problem_id: UUID
12+
machine-image-id: image-sc0
13+
- problem_id: UUID
14+
machine-image-id: image-sc1

pkg/command/command.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func NewNetconCommand() *cobra.Command {
1717
NewSchedulerCommand(),
1818
NewScoreserverCommand(),
1919
NewVmmsCommand(),
20+
NewContestCommand(),
2021
)
2122

2223
return rootCmd

pkg/command/contest.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package command
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
7+
"github.com/janog-netcon/netcon-cli/pkg/vmms"
8+
"github.com/spf13/cobra"
9+
"golang.org/x/xerrors"
10+
"gopkg.in/yaml.v2"
11+
)
12+
13+
func NewContestCommand() *cobra.Command {
14+
cmd := &cobra.Command{
15+
Use: "contest",
16+
}
17+
18+
cmd.AddCommand(
19+
NewContestInitCommand(),
20+
)
21+
22+
flags := cmd.PersistentFlags()
23+
flags.StringP("vmms-endpoint", "", "http://127.0.0.1:8950", "vm-management-server Endpoint")
24+
flags.StringP("vmms-credential", "", "", "Token")
25+
26+
return cmd
27+
}
28+
29+
func NewContestInitCommand() *cobra.Command {
30+
cmd := &cobra.Command{
31+
Use: "init",
32+
RunE: contestInitCommandFunc,
33+
}
34+
35+
flags := cmd.Flags()
36+
flags.StringP("mapping-file-path", "", "", "problem-idとmachine-image-idのマッピング情報が書いてあるファイルを指定する")
37+
flags.UintP("count", "c", 1, "何台ずつ作成するか")
38+
39+
cmd.MarkFlagRequired("mapping-file-path")
40+
41+
return cmd
42+
}
43+
44+
type mapping struct {
45+
ProblemID string `yaml:"problem_id"`
46+
MachineImageName string `yaml:"machine_image_name"`
47+
}
48+
49+
func contestInitCommandFunc(cmd *cobra.Command, args []string) error {
50+
flags := cmd.Flags()
51+
52+
vmmsEndpoint, err := flags.GetString("vmms-endpoint")
53+
if err != nil {
54+
return err
55+
}
56+
vmmsCredential, err := flags.GetString("vmms-credential")
57+
if err != nil {
58+
return err
59+
}
60+
mappingFilePath, err := flags.GetString("mapping-file-path")
61+
if err != nil {
62+
return err
63+
}
64+
count, err := flags.GetUint("count")
65+
if err != nil {
66+
return err
67+
}
68+
69+
// read mapping file
70+
bytes, err := ioutil.ReadFile(mappingFilePath)
71+
if err != nil {
72+
return err
73+
}
74+
75+
ml := []mapping{}
76+
if err := yaml.Unmarshal(bytes, &ml); err != nil {
77+
return err
78+
}
79+
80+
fmt.Printf("[INFO] read success: %#v\n", ml)
81+
82+
// validate
83+
for _, m := range ml {
84+
if m.ProblemID == "" {
85+
xerrors.New("problem_id が空になっている場所があります")
86+
}
87+
if m.MachineImageName == "" {
88+
xerrors.New("machine-image-name が空になっている場所にあります")
89+
}
90+
}
91+
92+
// create instance
93+
cli := vmms.NewClient(vmmsEndpoint, vmmsCredential)
94+
95+
// 問題ごとに指定カウント分作成させた方が、途中でコケたときに扱いやすい
96+
// 作成が完了した問題は設定ファイルから削除すればよくなる
97+
for _, m := range ml {
98+
for count > 0 {
99+
100+
fmt.Printf("[INFO] creating... problemID: %s, machineImageName: %s\n", m.ProblemID, m.MachineImageName)
101+
i, err := cli.CreateInstance(m.ProblemID, m.MachineImageName)
102+
if err != nil {
103+
fmt.Println("[ERROR] failed to create instance.")
104+
return err
105+
}
106+
fmt.Printf("[INFO] created: %#v\n", i)
107+
108+
count--
109+
}
110+
111+
}
112+
113+
fmt.Println("[INFO] success!!!!")
114+
115+
return nil
116+
}

0 commit comments

Comments
 (0)