This repository was archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathcreate.go
89 lines (81 loc) · 2.03 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package client
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
gflag "github.com/jessevdk/go-flags"
apitype "github.com/hyperhq/hyperd/types"
)
func (cli *HyperClient) HyperCmdCreate(args ...string) error {
var (
parser *gflag.Parser
opts = &CreateFlags{}
err error
podId string
)
parser = gflag.NewParser(opts, gflag.Default|gflag.IgnoreUnknown|gflag.PassAfterNonOption)
parser.Usage = "create [OPTIONS] [POD_ID] IMAGE [COMMAND] [ARG...]\n\nCreate a pod, or create a container in the pod specified by the POD_ID"
args, err = parser.ParseArgs(args)
if err != nil {
if !strings.Contains(err.Error(), "Usage") {
return err
} else {
return nil
}
}
if opts.Container {
if len(args) == 0 {
return fmt.Errorf("%s: \"create\" requires the pod ID as first argument.", os.Args[0])
}
podId = args[0]
args = args[1:]
}
specjson, err := cli.ParseCommonOptions(&opts.CommonFlags, opts.Container, args...)
if err != nil {
return err
}
if !opts.Container {
var tmpPod apitype.UserPod
err := json.Unmarshal(specjson, &tmpPod)
if err != nil {
return fmt.Errorf("failed to read json: %v", err)
}
podId, statusCode, err := cli.client.CreatePod(&tmpPod)
if err != nil {
if statusCode == http.StatusNotFound {
err = cli.PullImages(&tmpPod)
if err != nil {
return err
}
podId, statusCode, err = cli.client.CreatePod(&tmpPod)
}
if err != nil {
return err
}
}
fmt.Printf("Pod ID is %s\n", podId)
} else {
var tmpContainer apitype.UserContainer
err := json.Unmarshal(specjson, &tmpContainer)
if err != nil {
return fmt.Errorf("failed to read json: %v", err)
}
cid, statusCode, err := cli.client.CreateContainer(podId, &tmpContainer)
if err != nil {
if statusCode == http.StatusNotFound {
err = cli.PullImage(tmpContainer.Image)
if err != nil {
return err
}
cid, statusCode, err = cli.client.CreateContainer(podId, &tmpContainer)
}
if err != nil {
return err
}
}
fmt.Printf("Container ID is %s\n", cid)
}
return nil
}