func (mb MarathonBackend) UpdateDeployment(req *backend.UpdateRequest) (string, error) { glog.Infof("Updating a previously deployed application") app := marathon.NewDockerApplication() id := req.Name ports := req.Ports cpu := float64(req.CPULimit) storage := 0.0 memory := float64(req.MemoryLimit) labels := req.Labels imageurl := req.ImageURL env := req.Env replicas := req.Replicas app.Name(id) app.Uris = strings.Fields(DOCKERCFG) app.CPU(cpu) app.Memory(memory) app.Storage(storage) app.Count(replicas) app.Env = env portmappings := make([]*marathon.PortMapping, 0, len(ports)) for _, port := range ports { portmappings = append(portmappings, &marathon.PortMapping{ContainerPort: port, HostPort: 0, Protocol: "tcp"}) //TODO: change to protocol params, we probably want to have UDP too. } app.Container.Docker.PortMappings = portmappings app.Labels = labels app.Container.Docker.Container(imageurl).ForcePullImage = true appID, err := mb.Client.UpdateApplication(app) return appID.DeploymentID, err }
// Deploy deploys a new application // takes CreateRequest from backend as argument func (mb *MarathonBackend) Deploy(cr *CreateRequest) (string, error) { glog.Infof("Deploying a new application with name %s", cr.Name) app := marathon.NewDockerApplication() id := cr.Name ports := cr.Ports cpu := float64(cr.CPULimit) storage := 0.0 //TODO: setup limit for storage memory := float64(cr.MemoryLimit) labels := cr.Labels imageurl := cr.ImageURL env := cr.Env replicas := cr.Replicas app.Name(id) uris := strings.Fields(DOCKERCFG) app.Uris = &uris app.CPU(cpu).Memory(memory).Storage(storage).Count(replicas) app.Env = &env portmappings := make([]marathon.PortMapping, 0, len(ports)) for _, port := range ports { portmappings = append(portmappings, marathon.PortMapping{ContainerPort: port, HostPort: 0, Protocol: "tcp"}) //TODO: change to protocol params, we probably want to have UDP too. } app.Container.Docker.PortMappings = &portmappings //fluentd implementation if conf.New().FluentdEnabled { app.Container.Docker.AddParameter("log-driver", "fluentd") app.Container.Docker.AddParameter("log-opt", "\"fluentd-address=localhost:24224\"") if DOCKERVERSION == "1.9" { //this is unsupported if docker < 1.9 app.Container.Docker.AddParameter("log-opt", "\"tag={{.ImageName}}/{{.Name}}\"") } else { app.Container.Docker.AddParameter("log-opt", "\"fluentd-tag={{.Name}}\"") } } app.Labels = &labels forcepull := true app.Container.Docker.Container(imageurl).ForcePullImage = &forcepull volumes := make([]marathon.Volume, 0, len(cr.Volumes)) for _, volume := range cr.Volumes { volumes = append(volumes, marathon.Volume{ContainerPath: volume.ContainerPath, HostPath: volume.HostPath, Mode: volume.Mode}) } app.Container.Volumes = &volumes //TODO must implement configurable health checks application, err := mb.Client.CreateApplication(app) glog.Info(application) //TODO do we want to get some more information? Container IDs? I guess they can be not stable if err != nil { glog.Errorf("Could not create application %s, error %s", app.ID, err) return "", err } glog.Infof("Application was created, %s", app.ID) return app.ID, nil }
// Deploy deploys a new application // takes CreateRequest from backend as argument func (mb MarathonBackend) Deploy(cr *backend.CreateRequest) (string, error) { glog.Infof("Deploying a new application with name %s", cr.Name) app := marathon.NewDockerApplication() id := cr.Name ports := cr.Ports cpu := float64(cr.CPULimit) storage := 0.0 //TODO: setup limit for storage memory := float64(cr.MemoryLimit) labels := cr.Labels imageurl := cr.ImageURL env := cr.Env replicas := cr.Replicas app.Name(id) app.Uris = strings.Fields(DOCKERCFG) app.CPU(cpu).Memory(memory).Storage(storage).Count(replicas) app.Env = env portmappings := make([]*marathon.PortMapping, 0, len(ports)) for _, port := range ports { portmappings = append(portmappings, &marathon.PortMapping{ContainerPort: port, HostPort: 0, Protocol: "tcp"}) //TODO: change to protocol params, we probably want to have UDP too. } app.Container.Docker.PortMappings = portmappings //fluentd implementation if conf.New().FluentdEnabled { app.Container.Docker.Parameter("log-driver", "fluentd") app.Container.Docker.Parameter("log-opt", "\"fluentd-address=localhost:24224\"") app.Container.Docker.Parameter("log-opt", "\"fluentd-tag={{.Name}}\"") } app.Labels = labels app.Container.Docker.Container(imageurl).ForcePullImage = true //forcing basic health checks by default. //TODO must be configurable later. checks := make([]*marathon.HealthCheck, 0, 2) httpHealth := marathon.HealthCheck{Protocol: "HTTP", Path: "/health", GracePeriodSeconds: 3, IntervalSeconds: 10, MaxConsecutiveFailures: 10} cmdHealth := marathon.HealthCheck{Protocol: "COMMAND", Command: &marathon.Command{Value: "curl -f -X GET http://$HOST:$PORT0/health"}, MaxConsecutiveFailures: 10} checks = append(checks, &httpHealth) checks = append(checks, &cmdHealth) //app.HealthChecks = checks //FIXME health check has been removed because they were constantly killing the instances. application, err := mb.Client.CreateApplication(app) glog.Info(application) //TODO do we want to get some more information? Container IDs? I guess they can be not stable if err != nil { glog.Errorf("Could not create application %s, error %s", app.ID, err) return "", err } glog.Infof("Application was created, %s", app.ID) return app.ID, nil }
func main() { flag.Parse() config := marathon.NewDefaultConfig() config.URL = marathonURL client, err := marathon.NewClient(config) if err != nil { log.Fatalf("Failed to create a client for marathon, error: %s", err) } log.Printf("Retrieving a list of groups") if groups, err := client.Groups(); err != nil { log.Fatalf("Failed to retrieve the groups from maratho, error: %s", err) } else { for _, group := range groups.Groups { log.Printf("Found group: %s", group.ID) } } groupName := "/product/group" found, err := client.HasGroup(groupName) assert(err) if found { log.Printf("Deleting the group: %s, as it already exists", groupName) id, err := client.DeleteGroup(groupName) assert(err) err = client.WaitOnDeployment(id.DeploymentID, 0) assert(err) } /* step: the frontend app */ frontend := marathon.NewDockerApplication() frontend.Name("/product/group/frontend") frontend.CPU(0.1).Memory(64).Storage(0.0).Count(2) frontend.Arg("/usr/sbin/apache2ctl").Arg("-D").Arg("FOREGROUND") frontend.AddEnv("NAME", "frontend_http") frontend.AddEnv("SERVICE_80_NAME", "frontend_http") frontend.AddEnv("SERVICE_443_NAME", "frontend_https") frontend.AddEnv("BACKEND_MYSQL", "/product/group/mysql/3306;3306") frontend.AddEnv("BACKEND_CACHE", "/product/group/cache/6379;6379") frontend.DependsOn("/product/group/cache") frontend.DependsOn("/product/group/mysql") frontend.Container.Docker.Container("quay.io/gambol99/apache-php:latest").Expose(80).Expose(443) _, err = frontend.CheckHTTP("/hostname.php", 80, 10) assert(err) mysql := marathon.NewDockerApplication() mysql.Name("/product/group/mysql") mysql.CPU(0.1).Memory(128).Storage(0.0).Count(1) mysql.AddEnv("NAME", "group_cache") mysql.AddEnv("SERVICE_3306_NAME", "mysql") mysql.AddEnv("MYSQL_PASS", "mysql") mysql.Container.Docker.Container("tutum/mysql").Expose(3306) _, err = mysql.CheckTCP(3306, 10) assert(err) redis := marathon.NewDockerApplication() redis.Name("/product/group/cache") redis.CPU(0.1).Memory(64).Storage(0.0).Count(2) redis.AddEnv("NAME", "group_cache") redis.AddEnv("SERVICE_6379_NAME", "redis") redis.Container.Docker.Container("redis:latest").Expose(6379) _, err = redis.CheckTCP(6379, 10) assert(err) group := marathon.NewApplicationGroup(groupName) group.App(frontend).App(redis).App(mysql) assert(client.CreateGroup(group)) log.Printf("Successfully created the group: %s", group.ID) log.Printf("Updating the group paramaters") frontend.Count(4) id, err := client.UpdateGroup(groupName, group) assert(err) log.Printf("Successfully updated the group: %s, version: %s", group.ID, id.DeploymentID) assert(client.WaitOnGroup(groupName, 500*time.Second)) }
func main() { flag.Parse() config := marathon.NewDefaultConfig() config.URL = marathon_url config.LogOutput = os.Stdout client, err := marathon.NewClient(config) Assert(err) applications, err := client.Applications(nil) Assert(err) glog.Infof("Found %d application running", len(applications.Apps)) for _, application := range applications.Apps { glog.Infof("Application: %s", application) details, err := client.Application(application.ID) Assert(err) if details.Tasks != nil && len(details.Tasks) > 0 { for _, task := range details.Tasks { glog.Infof("task: %s", task) } health, err := client.ApplicationOK(details.ID) Assert(err) glog.Infof("Application: %s, healthy: %t", details.ID, health) } } APPLICATION_NAME := "/my/product" if found, _ := client.HasApplication(APPLICATION_NAME); found { deployId, err := client.DeleteApplication(APPLICATION_NAME) Assert(err) waitOnDeployment(client, deployId) } glog.Infof("Deploying a new application") application := marathon.NewDockerApplication() application.Name(APPLICATION_NAME) application.CPU(0.1).Memory(64).Storage(0.0).Count(2) application.Arg("/usr/sbin/apache2ctl").Arg("-D").Arg("FOREGROUND") application.AddEnv("NAME", "frontend_http") application.AddEnv("SERVICE_80_NAME", "test_http") application.RequirePorts = true application.Container.Docker.Container("quay.io/gambol99/apache-php:latest").Expose(80).Expose(443) _, err = client.CreateApplication(application) Assert(err) glog.Infof("Scaling the application to 4 instances") deployId, err := client.ScaleApplicationInstances(application.ID, 4, false) Assert(err) client.WaitOnApplication(application.ID, 30*time.Second) glog.Infof("Successfully scaled the application, deployId: %s", deployId.DeploymentID) glog.Infof("Deleting the application: %s", APPLICATION_NAME) deployId, err = client.DeleteApplication(application.ID) Assert(err) time.Sleep(time.Duration(10) * time.Second) glog.Infof("Successfully deleted the application") glog.Infof("Starting the application again") _, err = client.CreateApplication(application) Assert(err) glog.Infof("Created the application: %s", application.ID) glog.Infof("Delete all the tasks") _, err = client.KillApplicationTasks(application.ID, "", false) Assert(err) }
func main() { flag.Parse() config := marathon.NewDefaultConfig() config.URL = marathonURL client, err := marathon.NewClient(config) assert(err) applications, err := client.Applications(nil) assert(err) log.Printf("Found %d application running", len(applications.Apps)) for _, application := range applications.Apps { log.Printf("Application: %v", application) details, err := client.Application(application.ID) assert(err) if details.Tasks != nil && len(details.Tasks) > 0 { for _, task := range details.Tasks { log.Printf("task: %v", task) } health, err := client.ApplicationOK(details.ID) assert(err) log.Printf("Application: %s, healthy: %t", details.ID, health) } } applicationName := "/my/product" if _, err := client.Application(applicationName); err == nil { deployID, err := client.DeleteApplication(applicationName, false) assert(err) waitOnDeployment(client, deployID) } log.Printf("Deploying a new application") application := marathon.NewDockerApplication(). Name(applicationName). CPU(0.1). Memory(64). Storage(0.0). Count(2). AddArgs("/usr/sbin/apache2ctl", "-D", "FOREGROUND"). AddEnv("NAME", "frontend_http"). AddEnv("SERVICE_80_NAME", "test_http") application. Container.Docker.Container("quay.io/gambol99/apache-php:latest"). Bridged(). Expose(80). Expose(443) *application.RequirePorts = true _, err = client.CreateApplication(application) assert(err) client.WaitOnApplication(application.ID, 30*time.Second) log.Printf("Scaling the application to 4 instances") deployID, err := client.ScaleApplicationInstances(application.ID, 4, false) assert(err) client.WaitOnApplication(application.ID, 30*time.Second) log.Printf("Successfully scaled the application, deployID: %s", deployID.DeploymentID) log.Printf("Deleting the application: %s", applicationName) deployID, err = client.DeleteApplication(application.ID, true) assert(err) waitOnDeployment(client, deployID) log.Printf("Successfully deleted the application") log.Printf("Starting the application again") _, err = client.CreateApplication(application) assert(err) log.Printf("Created the application: %s", application.ID) log.Printf("Delete all the tasks") _, err = client.KillApplicationTasks(application.ID, nil) assert(err) }