Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lain_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__version__ = '2.3.11'
__version__ = '2.4.0'
4 changes: 3 additions & 1 deletion lain_cli/lain.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
from lain_cli.backup import BackupCommands
from lain_cli.maintainer import MaintainerCommands

from lain_cli.new import new

from lain_cli.utils import exit_gracefully

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

one_level_commands = [
appversion, attach, build, check, clear, dashboard, debug,
deploy, enter, login, logout, meta, prepare, prepare_update,
deploy, enter, login, logout, meta, new, prepare, prepare_update,
ps, push, refresh, reposit, rm, rmi, run, sync,
scale, stop, tag, test, undeploy, update, validate, version
]
Expand Down
2 changes: 1 addition & 1 deletion lain_cli/logout.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def logout(phase):
domain = get_domain(phase)
logout_success = SSOAccess.clear_token(phase)
if logout_success:
docker.logout('registry.%s'%domain)
docker.logout('registry.%s'%domain)
info("Logout successfully!")
else:
warn('Logout failed!')
93 changes: 93 additions & 0 deletions lain_cli/new.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# -*- coding: utf-8 -*-

import os
import subprocess
import sys
from argh.decorators import arg
from jinja2 import Environment, PackageLoader
from lain_sdk.util import error, info

TEMPLATE_CHOICES = ["go", "java", "python"]
env = Environment(loader=PackageLoader("lain_cli", "templates"))


@arg("appname", help="the name of the new LAIN app")
@arg("-t", "--template", choices=TEMPLATE_CHOICES, help="template to use")
def new(appname, template="go"):
"""
Create a LAIN project with template
"""
if appname == "":
error("appname should not be \"\".")
sys.exit(1)

try:
if template == "go":
new_go_project(appname)
elif template == "java":
new_java_project(appname)
elif template == "python":
new_python_project(appname)
else:
error("template should in {}.".format(TEMPLATE_CHOICES))
sys.exit(1)
except Exception as e:
error("{}.".format(e))
sys.exit(1)


def new_go_project(appname):
info("Creating {} with go template...".format(appname))
GOPATH = os.environ["GOPATH"]
GO_SRC_PATH = "{}/src/".format(GOPATH)
CWD = os.getcwd()
if not CWD.startswith(GO_SRC_PATH):
raise Exception("currenct working directory: {} is not in GOPATH: {}".
format(CWD, GOPATH))

if os.path.exists(appname):
raise Exception("directory or file: {} already exists".format(appname))

info("`git init {}` ...".format(appname))
subprocess.run(["git", "init", appname], check=True)
info("`git init {}` done.".format(appname))

with open("{}/lain.yaml".format(appname), "w+") as f:
data = env.get_template("go/lain.yaml.j2").render(
appname=appname, package_path=CWD.replace(GO_SRC_PATH, ""))
f.write(data)

with open("{}/main.go".format(appname), "w+") as f:
data = env.get_template("go/main.go").render()
f.write(data)

with open("{}/Gopkg.lock".format(appname), "w+") as f:
data = env.get_template("go/Gopkg.lock").render()
f.write(data)

with open("{}/Gopkg.toml".format(appname), "w+") as f:
data = env.get_template("go/Gopkg.toml").render()
f.write(data)

info("`git commit` ...")
os.chdir(appname)
subprocess.run(["git", "add", "-A"], check=True)
subprocess.run([
"git", "-c", "user.name=LAIN", "-c", "user.email=\"\"", "commit", "-m",
"initial commit"
],
check=True)
os.chdir(CWD)
info("`git commit` done.")

info("{} has been created with go template.".format(appname))


def new_java_project(appname):
info("Creating java project...")
info("Java project has been created.")


def new_python_project(appname):
info("Creating python project...")
info("Python project has been created.")
9 changes: 9 additions & 0 deletions lain_cli/templates/go/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions lain_cli/templates/go/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"

25 changes: 25 additions & 0 deletions lain_cli/templates/go/lain.yaml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
appname: {{ appname }}

build:
base: golang:1.9 # Or ${private_registry}/library/golang:1.9
prepare:
version: 201801081549 # Please increase ${version} to update prepare image
script:
- go get -u github.com/golang/dep/cmd/dep # Or any other package manager for golang
- mkdir -p $GOPATH/src/{{ package_path }}/{{ appname }}
- cp -rf . $GOPATH/src/{{ package_path }}/{{ appname }} # Copy code to $GOPATH/src
- cd $GOPATH/src/{{ package_path }}/{{ appname }} && dep ensure # Install dependencies by dep
script:
- cp -rf . $GOPATH/src/{{ package_path }}
- go install {{ package_path }}/{{ appname }}

release:
dest_base: laincloud/debian:stretch # Stdout/Stderr will be redirected to /lain/logs/default/currenct
copy:
- src: $GOPATH/bin/{{ appname }}
dest: /lain/app/{{ appname }}

web:
cmd: /lain/app/{{ appname }} # Please use absolute path
healthcheck: /ping

38 changes: 38 additions & 0 deletions lain_cli/templates/go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
)

func main() {
// Capture SIGTERM
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM)

mux := http.NewServeMux()

// Health check endpoint
mux.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
})

// Business logic endpoint
mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, world.\n"))
})

server := &http.Server{
Addr: ":8080",
Handler: mux,
}

go server.ListenAndServe()

<-quit
server.Shutdown(context.Background())
// Any other clean up actions...
}
20 changes: 20 additions & 0 deletions lain_cli/templates/python/lain.yaml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
appname: {{ appname }}

build:
base: laincloud/python:3.6 # Or ${private_registry}/library/python:3.6
# Stdout/Stderr will be redirected to /lain/logs/default/currenct
prepare:
version: 201801081549 # Please increase ${version} to update prepare image
script:
- go get -u github.com/golang/dep/cmd/dep # Or any other package manager for golang
- mkdir -p $GOPATH/src/{{ package_path }}/{{ appname }}
- cp -rf . $GOPATH/src/{{ package_path }}/{{ appname }} # Copy code to $GOPATH/src
- cd $GOPATH/src/{{ package_path }}/{{ appname }} && dep ensure # Install dependencies by dep
script:
- cp -rf . $GOPATH/src/{{ package_path }}
- go install {{ package_path }}/{{ appname }}

web:
cmd: /lain/app/{{ appname }} # Please use absolute path
healthcheck: /ping

1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""

requirements = [
'Jinja2>=2.10',
'PyYAML==3.11',
'argh==0.26.1',
'argcomplete==1.9.3',
Expand Down