From d83e2530902ef8020c00f26eb6d986963bf90217 Mon Sep 17 00:00:00 2001 From: bibaijin Date: Fri, 5 Jan 2018 18:47:31 +0800 Subject: [PATCH] add new command --- lain_cli/__init__.py | 2 +- lain_cli/lain.py | 4 +- lain_cli/logout.py | 2 +- lain_cli/new.py | 93 ++++++++++++++++++++++++++ lain_cli/templates/go/Gopkg.lock | 9 +++ lain_cli/templates/go/Gopkg.toml | 22 ++++++ lain_cli/templates/go/lain.yaml.j2 | 25 +++++++ lain_cli/templates/go/main.go | 38 +++++++++++ lain_cli/templates/python/lain.yaml.j2 | 20 ++++++ setup.py | 1 + 10 files changed, 213 insertions(+), 3 deletions(-) create mode 100644 lain_cli/new.py create mode 100644 lain_cli/templates/go/Gopkg.lock create mode 100644 lain_cli/templates/go/Gopkg.toml create mode 100644 lain_cli/templates/go/lain.yaml.j2 create mode 100644 lain_cli/templates/go/main.go create mode 100644 lain_cli/templates/python/lain.yaml.j2 diff --git a/lain_cli/__init__.py b/lain_cli/__init__.py index 25d5eb1..b9a8482 100644 --- a/lain_cli/__init__.py +++ b/lain_cli/__init__.py @@ -1,4 +1,4 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -__version__ = '2.3.11' +__version__ = '2.4.0' diff --git a/lain_cli/lain.py b/lain_cli/lain.py index 3d9c9da..64e0065 100755 --- a/lain_cli/lain.py +++ b/lain_cli/lain.py @@ -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) @@ -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 ] diff --git a/lain_cli/logout.py b/lain_cli/logout.py index f3aa5cd..7d7d118 100644 --- a/lain_cli/logout.py +++ b/lain_cli/logout.py @@ -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!') diff --git a/lain_cli/new.py b/lain_cli/new.py new file mode 100644 index 0000000..44fddad --- /dev/null +++ b/lain_cli/new.py @@ -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.") diff --git a/lain_cli/templates/go/Gopkg.lock b/lain_cli/templates/go/Gopkg.lock new file mode 100644 index 0000000..bef2d00 --- /dev/null +++ b/lain_cli/templates/go/Gopkg.lock @@ -0,0 +1,9 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "ab4fef131ee828e96ba67d31a7d690bd5f2f42040c6766b1b12fe856f87e0ff7" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/lain_cli/templates/go/Gopkg.toml b/lain_cli/templates/go/Gopkg.toml new file mode 100644 index 0000000..9425a54 --- /dev/null +++ b/lain_cli/templates/go/Gopkg.toml @@ -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" + diff --git a/lain_cli/templates/go/lain.yaml.j2 b/lain_cli/templates/go/lain.yaml.j2 new file mode 100644 index 0000000..50df06a --- /dev/null +++ b/lain_cli/templates/go/lain.yaml.j2 @@ -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 + diff --git a/lain_cli/templates/go/main.go b/lain_cli/templates/go/main.go new file mode 100644 index 0000000..ca5fe5b --- /dev/null +++ b/lain_cli/templates/go/main.go @@ -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... +} diff --git a/lain_cli/templates/python/lain.yaml.j2 b/lain_cli/templates/python/lain.yaml.j2 new file mode 100644 index 0000000..d7a7ab9 --- /dev/null +++ b/lain_cli/templates/python/lain.yaml.j2 @@ -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 + diff --git a/setup.py b/setup.py index bfb09af..bb5164c 100644 --- a/setup.py +++ b/setup.py @@ -8,6 +8,7 @@ """ requirements = [ + 'Jinja2>=2.10', 'PyYAML==3.11', 'argh==0.26.1', 'argcomplete==1.9.3',