Skip to content

Commit d83e253

Browse files
committed
add new command
1 parent d1b18ae commit d83e253

File tree

10 files changed

+213
-3
lines changed

10 files changed

+213
-3
lines changed

lain_cli/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
__version__ = '2.3.11'
4+
__version__ = '2.4.0'

lain_cli/lain.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
from lain_cli.backup import BackupCommands
4141
from lain_cli.maintainer import MaintainerCommands
4242

43+
from lain_cli.new import new
44+
4345
from lain_cli.utils import exit_gracefully
4446

4547
logging.getLogger("requests").setLevel(logging.WARNING)
@@ -49,7 +51,7 @@
4951

5052
one_level_commands = [
5153
appversion, attach, build, check, clear, dashboard, debug,
52-
deploy, enter, login, logout, meta, prepare, prepare_update,
54+
deploy, enter, login, logout, meta, new, prepare, prepare_update,
5355
ps, push, refresh, reposit, rm, rmi, run, sync,
5456
scale, stop, tag, test, undeploy, update, validate, version
5557
]

lain_cli/logout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def logout(phase):
1717
domain = get_domain(phase)
1818
logout_success = SSOAccess.clear_token(phase)
1919
if logout_success:
20-
docker.logout('registry.%s'%domain)
20+
docker.logout('registry.%s'%domain)
2121
info("Logout successfully!")
2222
else:
2323
warn('Logout failed!')

lain_cli/new.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import os
4+
import subprocess
5+
import sys
6+
from argh.decorators import arg
7+
from jinja2 import Environment, PackageLoader
8+
from lain_sdk.util import error, info
9+
10+
TEMPLATE_CHOICES = ["go", "java", "python"]
11+
env = Environment(loader=PackageLoader("lain_cli", "templates"))
12+
13+
14+
@arg("appname", help="the name of the new LAIN app")
15+
@arg("-t", "--template", choices=TEMPLATE_CHOICES, help="template to use")
16+
def new(appname, template="go"):
17+
"""
18+
Create a LAIN project with template
19+
"""
20+
if appname == "":
21+
error("appname should not be \"\".")
22+
sys.exit(1)
23+
24+
try:
25+
if template == "go":
26+
new_go_project(appname)
27+
elif template == "java":
28+
new_java_project(appname)
29+
elif template == "python":
30+
new_python_project(appname)
31+
else:
32+
error("template should in {}.".format(TEMPLATE_CHOICES))
33+
sys.exit(1)
34+
except Exception as e:
35+
error("{}.".format(e))
36+
sys.exit(1)
37+
38+
39+
def new_go_project(appname):
40+
info("Creating {} with go template...".format(appname))
41+
GOPATH = os.environ["GOPATH"]
42+
GO_SRC_PATH = "{}/src/".format(GOPATH)
43+
CWD = os.getcwd()
44+
if not CWD.startswith(GO_SRC_PATH):
45+
raise Exception("currenct working directory: {} is not in GOPATH: {}".
46+
format(CWD, GOPATH))
47+
48+
if os.path.exists(appname):
49+
raise Exception("directory or file: {} already exists".format(appname))
50+
51+
info("`git init {}` ...".format(appname))
52+
subprocess.run(["git", "init", appname], check=True)
53+
info("`git init {}` done.".format(appname))
54+
55+
with open("{}/lain.yaml".format(appname), "w+") as f:
56+
data = env.get_template("go/lain.yaml.j2").render(
57+
appname=appname, package_path=CWD.replace(GO_SRC_PATH, ""))
58+
f.write(data)
59+
60+
with open("{}/main.go".format(appname), "w+") as f:
61+
data = env.get_template("go/main.go").render()
62+
f.write(data)
63+
64+
with open("{}/Gopkg.lock".format(appname), "w+") as f:
65+
data = env.get_template("go/Gopkg.lock").render()
66+
f.write(data)
67+
68+
with open("{}/Gopkg.toml".format(appname), "w+") as f:
69+
data = env.get_template("go/Gopkg.toml").render()
70+
f.write(data)
71+
72+
info("`git commit` ...")
73+
os.chdir(appname)
74+
subprocess.run(["git", "add", "-A"], check=True)
75+
subprocess.run([
76+
"git", "-c", "user.name=LAIN", "-c", "user.email=\"\"", "commit", "-m",
77+
"initial commit"
78+
],
79+
check=True)
80+
os.chdir(CWD)
81+
info("`git commit` done.")
82+
83+
info("{} has been created with go template.".format(appname))
84+
85+
86+
def new_java_project(appname):
87+
info("Creating java project...")
88+
info("Java project has been created.")
89+
90+
91+
def new_python_project(appname):
92+
info("Creating python project...")
93+
info("Python project has been created.")

lain_cli/templates/go/Gopkg.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lain_cli/templates/go/Gopkg.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
# Gopkg.toml example
3+
#
4+
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
5+
# for detailed Gopkg.toml documentation.
6+
#
7+
# required = ["github.com/user/thing/cmd/thing"]
8+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
9+
#
10+
# [[constraint]]
11+
# name = "github.com/user/project"
12+
# version = "1.0.0"
13+
#
14+
# [[constraint]]
15+
# name = "github.com/user/project2"
16+
# branch = "dev"
17+
# source = "github.com/myfork/project2"
18+
#
19+
# [[override]]
20+
# name = "github.com/x/y"
21+
# version = "2.4.0"
22+

lain_cli/templates/go/lain.yaml.j2

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
appname: {{ appname }}
2+
3+
build:
4+
base: golang:1.9 # Or ${private_registry}/library/golang:1.9
5+
prepare:
6+
version: 201801081549 # Please increase ${version} to update prepare image
7+
script:
8+
- go get -u github.com/golang/dep/cmd/dep # Or any other package manager for golang
9+
- mkdir -p $GOPATH/src/{{ package_path }}/{{ appname }}
10+
- cp -rf . $GOPATH/src/{{ package_path }}/{{ appname }} # Copy code to $GOPATH/src
11+
- cd $GOPATH/src/{{ package_path }}/{{ appname }} && dep ensure # Install dependencies by dep
12+
script:
13+
- cp -rf . $GOPATH/src/{{ package_path }}
14+
- go install {{ package_path }}/{{ appname }}
15+
16+
release:
17+
dest_base: laincloud/debian:stretch # Stdout/Stderr will be redirected to /lain/logs/default/currenct
18+
copy:
19+
- src: $GOPATH/bin/{{ appname }}
20+
dest: /lain/app/{{ appname }}
21+
22+
web:
23+
cmd: /lain/app/{{ appname }} # Please use absolute path
24+
healthcheck: /ping
25+

lain_cli/templates/go/main.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"os"
7+
"os/signal"
8+
"syscall"
9+
)
10+
11+
func main() {
12+
// Capture SIGTERM
13+
quit := make(chan os.Signal, 1)
14+
signal.Notify(quit, syscall.SIGTERM)
15+
16+
mux := http.NewServeMux()
17+
18+
// Health check endpoint
19+
mux.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
20+
w.Write([]byte("OK"))
21+
})
22+
23+
// Business logic endpoint
24+
mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
25+
w.Write([]byte("Hello, world.\n"))
26+
})
27+
28+
server := &http.Server{
29+
Addr: ":8080",
30+
Handler: mux,
31+
}
32+
33+
go server.ListenAndServe()
34+
35+
<-quit
36+
server.Shutdown(context.Background())
37+
// Any other clean up actions...
38+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
appname: {{ appname }}
2+
3+
build:
4+
base: laincloud/python:3.6 # Or ${private_registry}/library/python:3.6
5+
# Stdout/Stderr will be redirected to /lain/logs/default/currenct
6+
prepare:
7+
version: 201801081549 # Please increase ${version} to update prepare image
8+
script:
9+
- go get -u github.com/golang/dep/cmd/dep # Or any other package manager for golang
10+
- mkdir -p $GOPATH/src/{{ package_path }}/{{ appname }}
11+
- cp -rf . $GOPATH/src/{{ package_path }}/{{ appname }} # Copy code to $GOPATH/src
12+
- cd $GOPATH/src/{{ package_path }}/{{ appname }} && dep ensure # Install dependencies by dep
13+
script:
14+
- cp -rf . $GOPATH/src/{{ package_path }}
15+
- go install {{ package_path }}/{{ appname }}
16+
17+
web:
18+
cmd: /lain/app/{{ appname }} # Please use absolute path
19+
healthcheck: /ping
20+

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"""
99

1010
requirements = [
11+
'Jinja2>=2.10',
1112
'PyYAML==3.11',
1213
'argh==0.26.1',
1314
'argcomplete==1.9.3',

0 commit comments

Comments
 (0)