Skip to content

Commit 46160aa

Browse files
committed
initial
1 parent 17d5c76 commit 46160aa

File tree

11 files changed

+933
-0
lines changed

11 files changed

+933
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_exe

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
SHELL=/bin/bash
2+
3+
install: build
4+
scripts/build/install
5+
6+
build:
7+
scripts/build/plugin
8+
9+
.PHONY: build install clean

go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module github.com/liquidweb/docker-machine-driver-liquidweb
2+
3+
go 1.15
4+
5+
require (
6+
github.com/docker/docker v20.10.2+incompatible // indirect
7+
github.com/docker/machine v0.16.2
8+
github.com/liquidweb/liquidweb-go v1.6.1
9+
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect
10+
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
11+
)
12+
13+
replace github.com/liquidweb/liquidweb-go => /home/ssullivan/golang/src/github.com/liquidweb/liquidweb-go

go.sum

Lines changed: 370 additions & 0 deletions
Large diffs are not rendered by default.

lwdriver/core.go

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
Copyright © LiquidWeb
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package lwdriver
17+
18+
import (
19+
"errors"
20+
"io/ioutil"
21+
"strings"
22+
"time"
23+
24+
"github.com/docker/machine/libmachine/drivers"
25+
"github.com/docker/machine/libmachine/log"
26+
"github.com/docker/machine/libmachine/mcnflag"
27+
"github.com/docker/machine/libmachine/ssh"
28+
29+
lwclient "github.com/liquidweb/liquidweb-go/client"
30+
31+
"github.com/liquidweb/docker-machine-driver-liquidweb/util"
32+
)
33+
34+
const (
35+
DefaultTemplate string = "DEBIAN_10_UNMANAGED"
36+
DefaultDockerPort int = 2376
37+
)
38+
39+
func NewDriver() *Driver {
40+
return &Driver{
41+
BaseDriver: &drivers.BaseDriver{
42+
SSHUser: drivers.DefaultSSHUser,
43+
SSHPort: drivers.DefaultSSHPort,
44+
},
45+
}
46+
}
47+
48+
func (self *Driver) DriverName() string {
49+
return "liquidweb"
50+
}
51+
52+
func (self *Driver) GetCreateFlags() []mcnflag.Flag {
53+
return []mcnflag.Flag{
54+
mcnflag.StringFlag{
55+
EnvVar: "LW_USERNAME",
56+
Name: "lw-username",
57+
Usage: "liquidweb api/account username",
58+
Value: "",
59+
},
60+
61+
mcnflag.StringFlag{
62+
EnvVar: "LW_PASSWORD",
63+
Name: "lw-password",
64+
Usage: "password associated with lw-username",
65+
Value: "",
66+
},
67+
68+
mcnflag.StringFlag{
69+
EnvVar: "LW_API_DOMAIN",
70+
Name: "lw-api-domain",
71+
Usage: "liquidweb public api domain",
72+
Value: "https://api.liquidweb.com",
73+
},
74+
75+
mcnflag.StringFlag{
76+
EnvVar: "LW_TEMPLATE",
77+
Name: "lw-template",
78+
Usage: "Name of the template to deploy on the node",
79+
Value: DefaultTemplate,
80+
},
81+
82+
mcnflag.StringFlag{
83+
EnvVar: "LW_NODE_ROOT_PASSWORD",
84+
Name: "lw-node-root-password",
85+
Usage: "root password to set on the node",
86+
Value: "",
87+
},
88+
89+
mcnflag.IntFlag{
90+
EnvVar: "LW_CONFIG_ID",
91+
Name: "lw-config-id",
92+
Usage: "config-id to deploy the node as",
93+
},
94+
95+
mcnflag.IntFlag{
96+
EnvVar: "LW_ZONE_ID",
97+
Name: "lw-zone-id",
98+
Usage: "zone_id of the zone to deploy the node in",
99+
Value: -1,
100+
},
101+
102+
mcnflag.StringFlag{
103+
EnvVar: "LW_NODE_HOSTNAME",
104+
Name: "lw-node-hostname",
105+
Usage: "hostname to give the node",
106+
Value: util.RandomHostname(),
107+
},
108+
109+
mcnflag.IntFlag{
110+
EnvVar: "LW_DOCKER_PORT",
111+
Name: "lw-docker-port",
112+
Usage: "dockerport to use",
113+
Value: DefaultDockerPort,
114+
},
115+
}
116+
}
117+
118+
func (self *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
119+
self.BaseDriver.SetSwarmConfigFromFlags(opts)
120+
121+
self.LwApiUsername = opts.String("lw-username")
122+
self.LwApiPassword = opts.String("lw-password")
123+
self.LwApiDomain = opts.String("lw-api-domain")
124+
125+
self.LwComputeConfigId = opts.Int("lw-config-id")
126+
self.LwComputeZoneId = opts.Int("lw-zone-id")
127+
self.LwComputeNodeHostname = opts.String("lw-node-hostname")
128+
self.LwComputeNodeRootPassword = opts.String("lw-node-root-password")
129+
self.LwComputeTemplate = opts.String("lw-template")
130+
131+
self.DockerPort = opts.Int("lw-docker-port")
132+
133+
if self.DockerPort <= 0 {
134+
return errors.New("docker port must be greater than zero")
135+
}
136+
137+
if self.LwApiUsername == "" || self.LwApiPassword == "" {
138+
return errors.New("lw username and password are required")
139+
}
140+
141+
if self.LwApiDomain == "" {
142+
return errors.New("api domain cannot be blank")
143+
}
144+
145+
if self.LwComputeTemplate == "" {
146+
return errors.New("template cannot be blank")
147+
}
148+
149+
if strings.Contains(strings.ToUpper(self.LwComputeTemplate), "WINDOWS") {
150+
return errors.New("Windows is not supported")
151+
}
152+
153+
return nil
154+
}
155+
156+
// private
157+
158+
func (self *Driver) waitUntilMachineNodeReady(api *lwclient.API) {
159+
var ready bool
160+
for !ready {
161+
status, err := api.StormServer.Status(self.LwComputeNodeUniqId)
162+
if err != nil {
163+
log.Warnf("failed fetching status for [%s]: %s", self.LwComputeNodeUniqId, err)
164+
time.Sleep(30 * time.Second)
165+
continue
166+
}
167+
if strings.ToUpper(status.Status) == "RUNNING" {
168+
log.Infof("node [%s] has become ready", self.LwComputeNodeUniqId)
169+
break
170+
} else {
171+
time.Sleep(10 * time.Second)
172+
}
173+
}
174+
}
175+
176+
func (self *Driver) getApiClient() (*lwclient.API, error) {
177+
user := self.LwApiUsername
178+
pass := self.LwApiPassword
179+
url := self.LwApiDomain
180+
api, err := lwclient.NewAPI(user, pass, url, 90)
181+
182+
return api, err
183+
}
184+
185+
func (self *Driver) createSshKey() (pubKey string, err error) {
186+
sshKeyPath := self.GetSSHKeyPath()
187+
if err = ssh.GenerateSSHKey(sshKeyPath); err != nil {
188+
return
189+
}
190+
191+
var pubKeyB []byte
192+
pubKeyB, err = ioutil.ReadFile(sshKeyPath + ".pub")
193+
pubKey = string(pubKeyB)
194+
195+
return
196+
}

0 commit comments

Comments
 (0)