forked from Juniper/contrail
/
deploy.go
152 lines (136 loc) · 4.34 KB
/
deploy.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package deploy
import (
"context"
"fmt"
"io/ioutil"
"github.com/Juniper/asf/pkg/keystone"
"github.com/Juniper/asf/pkg/logutil"
"github.com/Juniper/contrail/pkg/client"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
asfclient "github.com/Juniper/asf/pkg/client"
yaml "gopkg.in/yaml.v2"
)
// Config represents Deploy configuration.
type Config struct { // nolint: maligned
// ID of Deploy account.
ID string `yaml:"id"`
// Password of Deploy account.
Password string `yaml:"password"`
// DomainID is ID of keystone domain used for authentication.
DomainID string `yaml:"domain_id"`
// ProjectID is ID of keystone project used for authentication.
ProjectID string `yaml:"project_id"`
// DomainName is Name of keystone domain used for authentication.
DomainName string `yaml:"domain_name"`
// ProjectName is Name of keystone project used for authentication.
ProjectName string `yaml:"project_name"`
// AuthURL defines authentication URL.
AuthURL string `yaml:"auth_url"`
// Endpoint of API Server.
Endpoint string `yaml:"endpoint"`
// InSecure https connection to endpoint
InSecure bool `yaml:"insecure"`
// Resource type to be managed.
ResourceType string `yaml:"resource_type,omitempty"`
// UUID of resource to be managed.
ResourceID string `yaml:"resource_id,omitempty"`
// Action to the performed with the resource (values: create, update, delete).
Action string `yaml:"resource_action,omitempty"`
// Provisioning tool used to provision the resource (values: ansible, helm).
ProvisionerType string `yaml:"provisioner_type,omitempty"`
// Logging level
LogLevel string `yaml:"log_level"`
// Logging file
LogFile string `yaml:"log_file"`
// Template root directory
TemplateRoot string `yaml:"template_root"`
// Service user name for keystone
ServiceUserID string `yaml:"service_user_id"`
// Service user password for keystone
ServiceUserPassword string `yaml:"service_user_password"`
// Optional ansible sudo password
AnsibleSudoPass string `yaml:"ansible_sudo_pass"`
// Optional ansible deployer cherry pick url
AnsibleFetchURL string `yaml:"ansible_fetch_url"`
// Optional ansible deployer cherry pick revison(commit id)
AnsibleCherryPickRevision string `yaml:"ansible_cherry_pick_revision"`
// Optional ansible deployer revision(commit id)
AnsibleRevision string `yaml:"ansible_revision"`
// Optional Test var to run command in test mode
Test bool `yaml:"test"`
}
// Deploy represents Deploy service.
type Deploy struct {
managerType string
config *Config
APIServer *client.HTTP
log *logrus.Entry
streamServer *logutil.StreamServer
}
// NewDeployManager creates Deploy reading configuration from given file.
func NewDeployManager(configPath string) (*Deploy, error) {
data, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, err
}
var c Config
err = yaml.UnmarshalStrict(data, &c)
if err != nil {
return nil, err
}
return NewDeploy(&c)
}
// NewDeploy creates Deploy with given configuration.
func NewDeploy(c *Config) (*Deploy, error) {
if err := logutil.Configure(c.LogLevel); err != nil {
return nil, err
}
s := client.NewHTTP(&asfclient.HTTPConfig{
ID: c.ID,
Password: c.Password,
Endpoint: c.Endpoint,
AuthURL: c.AuthURL,
Scope: keystone.NewScope(
c.DomainID,
c.DomainName,
c.ProjectID,
c.ProjectName,
),
Insecure: c.InSecure,
})
t := "daemon"
if c.ResourceID != "" && c.Action != "" {
t = "oneshot"
} else if c.ResourceID != "" && c.Action == "" {
return nil, fmt.Errorf("action not specified in the config for oneshot manager")
} else if c.Action != "" && c.ResourceID == "" {
return nil, fmt.Errorf("resource ID not specified in the config for oneshot manager")
}
return &Deploy{
managerType: t,
APIServer: s,
config: c,
log: logutil.NewFileLogger("deploy", c.LogFile),
streamServer: logutil.NewStreamServer(c.LogFile),
}, nil
}
// Manage starts managing the resource.
func (c *Deploy) Manage() error {
c.streamServer.Serve()
defer c.streamServer.Close()
c.log.Infof("start handling %s", c.config.ResourceType)
if err := c.APIServer.Login(context.Background()); err != nil {
return errors.Wrap(err, "login to API Server failed")
}
manager, err := newManager(c)
if err != nil {
return err
}
err = manager.manage()
if err != nil {
return err
}
c.log.Infof("stop handling %s", c.config.ResourceType)
return nil
}