Example #1
0
func configDocker(d *model.Docker) error {
	var lastDocker model.Docker
	query := model.DB.Order("port desc").
		Not("port = ?", 0).
		Where("server_id = ?", d.ServerId).
		First(&lastDocker)
	if query.Error != nil && query.Error != gorm.RecordNotFound {
		return query.Error
	}
	if lastDocker.Port < 9000 {
		d.Port = 9000
	} else {
		d.Port = lastDocker.Port + 1
	}
	fmt.Printf("%d %d", lastDocker.Port, d.Port)
	return nil
}
Example #2
0
func StartBuild(b *model.Bao, server model.Server) error {
	var docker model.Docker
	docker.ServerId = server.Id

	err := configDocker(&docker)
	if err != nil {
		writeToBao(b, "Error configuring Docker: "+err.Error(), true)
		return nil
	}

	writeToBao(b, "Cloning gist files", false)
	directory, err := DownloadFromRepo(b)
	if err != nil {
		writeToBao(b, "Error cloning files", true)
		return err
	} else {
		writeToBao(b, "Files cloned successfully", false)
	}
	writeToBao(b, "Creating dockerfile", false)
	err = CreateDockerfile(directory)
	if err != nil {
		writeToBao(b, "Error creating dockerfile", true)
		return err
	}
	writeToBao(b, "Building dockerfile (this could take a while)", false)
	dockerId, err := BuildDockerfile(b, directory, docker)
	if err != nil {
		writeToBao(b, err.Error()+"\nquitting...", true)
		return err
	}

	docker.DockerId = dockerId
	docker.BaoId = b.Id

	b.Location.BaoId = b.Id
	b.Location.Subdomain = fmt.Sprintf("%s-%d", b.GistId, b.Id)
	b.Location.Destination = fmt.Sprintf("%s:%d", server.Ip, docker.Port)

	writeToBao(b, "Site hosted on: "+b.Location.Subdomain+".gitbao.com", true)
	model.DB.Save(b)
	model.DB.Create(&docker)

	err = os.RemoveAll(directory)

	return nil
}