예제 #1
0
func deployAction(*cli.Context) {
	_apps, err := apps.LinkedApps(".")
	utils.CheckError(err)
	if len(_apps) == 0 {
		log.Fatalln("没有关联任何 app,请使用 lean app add 来关联应用。")
	}

	// TODO: specific app
	app := _apps[0]

	appInfo, err := apps.GetAppInfo(app.AppID)
	utils.CheckError(err)

	groupName, err := deployGroupName(appInfo)
	utils.CheckError(err)

	if groupName == "staging" {
		log.Println("准备部署应用到预备环境")
	} else {
		log.Println("准备部署应用到生产环境: " + groupName)
	}

	if isDeployFromGit {
		deployFromGit(appInfo, groupName)
	} else {
		deployFromLocal(appInfo, groupName)
	}
}
예제 #2
0
func deployFromLocal(appInfo *apps.AppInfo, groupName string) {
	file, err := uploadProject(appInfo, "")
	utils.CheckError(err)

	client := api.NewKeyAuthClient(appInfo.AppID, appInfo.MasterKey)

	_, err = client.BuildFromURL(groupName, file.URL)
	utils.CheckError(err)

	err = client.DeleteFile(file.ID)
	utils.CheckError(err)
}
예제 #3
0
func appListAction(c *cli.Context) {
	appList, err := apps.LinkedApps("")
	utils.CheckError(err)

	currentAppName, err := apps.CurrentAppName("")
	utils.CheckError(err)

	for _, app := range appList {
		if currentAppName == app.AppName {
			fmt.Printf("* %s - %s \r\n", app.AppName, app.AppID)
		} else {
			fmt.Printf("  %s - %s \r\n", app.AppName, app.AppID)
		}
	}
}
예제 #4
0
// FetchRepo will download the boilerplate from remote and extract to ${appName}/ folder
func FetchRepo(t int, appName string, appID string) error {
	utils.CheckError(os.Mkdir(appName, 0700))

	repoURL := map[int]string{
		Python: "http://lcinternal-cloud-code-update.leanapp.cn/python-getting-started.zip",
		NodeJS: "http://lcinternal-cloud-code-update.leanapp.cn/node-js-getting-started.zip",
	}[t]

	dir, err := ioutil.TempDir("", "leanengine")
	utils.CheckError(err)
	defer os.RemoveAll(dir)

	log.Println("正在下载项目模版...")

	resp, err := grequests.Get(repoURL, nil)
	if err != nil {
		return err
	}
	defer resp.Close()
	if resp.StatusCode != 200 {
		return errors.New(utils.FormatServerErrorResult(resp.String()))
	}

	log.Println("下载完成")

	zipFilePath := filepath.Join(dir, "getting-started.zip")
	resp.DownloadToFile(zipFilePath)

	log.Println("正在创建项目...")

	zipFile, err := zip.OpenReader(zipFilePath)
	utils.CheckError(err)
	defer zipFile.Close()
	for _, f := range zipFile.File {
		err := extractAndWriteFile(f, appName)
		if err != nil {
			return err
		}
	}

	if err := apps.AddApp(appName, appName, appID); err != nil {
		return err
	}

	log.Println("创建项目完成")

	return nil
}
예제 #5
0
func uploadProject(appInfo *apps.AppInfo, repoPath string) (*api.File, error) {
	// TODO: ignore files

	fileDir, err := ioutil.TempDir("", "leanengine")
	if err != nil {
		return nil, err
	}

	filePath := filepath.Join(fileDir, "leanengine.zip")
	println(filePath)

	log.Println("压缩项目文件 ...")
	zip := new(archivex.ZipFile)
	func() {
		defer zip.Close()
		zip.Create(filePath)
		zip.AddAll(repoPath, false)
	}()

	log.Println("上传项目文件 ...")
	client := api.NewKeyAuthClient(appInfo.AppID, appInfo.MasterKey)
	file, err := client.UploadFile(filePath)
	utils.CheckError(err)

	return file, nil
}
예제 #6
0
func appRemoveAction(c *cli.Context) {
	listOfApps, err := apps.LinkedApps("")
	utils.CheckError(err)
	if len(listOfApps) == 0 {
		log.Fatal("没有关联任何应用")
	}

	answers := []wizard.Answer{}
	appToRemove := new(string)
	for _, app := range listOfApps {
		answers = append(answers, wizard.Answer{
			Content: fmt.Sprintf("%s - %s", app.AppName, app.AppID),
			Handler: func() {
				appToRemove = &app.AppName
			},
		})
	}

	wizard.Ask([]wizard.Question{
		{
			Content: "请选择要移除关联的应用:",
			Answers: answers,
		},
	})

	log.Println("移除关联的应用:", *appToRemove)
	apps.RemoveApp("", *appToRemove)
}
예제 #7
0
func appSwitchAction(c *cli.Context) {
	if c.NArg() != 1 {
		log.Fatal("Usage: lean app switch <app-name>")
	}
	appName := c.Args()[0]

	utils.CheckError(apps.SwitchApp("", appName))
}
예제 #8
0
func appAddAction(c *cli.Context) {
	var appName, appID string
	wizard.Ask([]wizard.Question{
		{
			Content: "请输入应用名:",
			Input: &wizard.Input{
				Hidden: false,
				Result: &appName,
			},
		},
		{
			Content: "请输入应用 appID:",
			Input: &wizard.Input{
				Hidden: false,
				Result: &appID,
			},
		},
	})

	utils.CheckError(apps.AddApp("", appName, appID))
	utils.CheckError(apps.SwitchApp("", appName))
	log.Println("已切换至应用:" + appName)
}
예제 #9
0
func newAction(*cli.Context) error {
	apps, err := api.GetAppList()
	if err != nil {
		return err
	}
	app := selectApp(apps)
	appID := app["app_id"].(string)
	masterKey := app["master_key"].(string)

	runtime := selectRuntime()

	client := api.NewKeyAuthClient(appID, masterKey)

	detail, err := client.AppDetail()
	utils.CheckError(err)
	appName := detail.Get("app_name").MustString()

	err = boilerplate.FetchRepo(runtime, appName, appID)
	return err
}
예제 #10
0
func deployFromGit(appInfo *apps.AppInfo, groupName string) {
	client := api.NewKeyAuthClient(appInfo.AppID, appInfo.MasterKey)

	_, err := client.BuildFromGit(groupName)
	utils.CheckError(err)
}