Example #1
0
func main() {
	flag.Parse()
	config := marathon.NewDefaultConfig()
	config.URL = marathon_url
	config.LogOutput = os.Stdout
	config.EventsPort = marathon_port
	config.EventsInterface = marathon_interface
	glog.Infof("Creating a client Marathon: %s", marathon_url)

	client, err := marathon.NewClient(config)
	Assert(err)

	/* step: lets register for events */
	events := make(marathon.EventsChannel, 5)
	deployments := make(marathon.EventsChannel, 5)
	Assert(client.AddEventsListener(events, marathon.EVENTS_APPLICATIONS))
	Assert(client.AddEventsListener(deployments, marathon.EVENT_DEPLOYMENT_STEP_SUCCESS))

	// lets listen for 10 seconds and then split
	timer := time.After(time.Duration(timeout) * time.Second)
	kill_off := false
	for {
		if kill_off {
			break
		}
		select {
		case <-timer:
			glog.Infof("Exitting the loop")
			kill_off = true
		case event := <-events:
			glog.Infof("Recieved application event: %s", event)
		case event := <-deployments:
			glog.Infof("Recieved deployment event: %v", event)
			var deployment *marathon.EventDeploymentStepSuccess
			deployment = event.Event.(*marathon.EventDeploymentStepSuccess)
			glog.Infof("deployment step:: %v", deployment.CurrentStep)
		}
	}

	glog.Infof("Removing our subscription")
	client.RemoveEventsListener(events)
	client.RemoveEventsListener(deployments)

	Assert(client.UnSubscribe())
}
Example #2
0
func main() {
	flag.Parse()
	config := marathon.NewDefaultConfig()
	config.URL = marathon_url
	config.LogOutput = os.Stdout
	client, err := marathon.NewClient(config)
	if err != nil {
		glog.Fatalf("Failed to create a client for marathon, error: %s", err)
	}
	for {
		if application, err := client.Applications(nil); err != nil {
			glog.Errorf("Failed to retrieve a list of applications, error: %s", err)
		} else {
			glog.Infof("Retrieved a list of applications, %v", application)
		}
		glog.Infof("Going to sleep for 20 seconds")
		time.Sleep(5 * time.Second)
	}
}
Example #3
0
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)
}
Example #4
0
func main() {
	flag.Parse()
	config := marathon.NewDefaultConfig()
	config.URL = marathon_url
	config.LogOutput = os.Stdout
	client, err := marathon.NewClient(config)
	if err != nil {
		glog.Fatalf("Failed to create a client for marathon, error: %s", err)
	}

	glog.Infof("Retrieving a list of groups")
	if groups, err := client.Groups(); err != nil {
		glog.Errorf("Failed to retrieve the groups from maratho, error: %s", err)
	} else {
		for _, group := range groups.Groups {
			glog.Infof("Found group: %s", group.ID)
		}
	}

	GROUP_NAME := "/product/group"

	found, err := client.HasGroup(GROUP_NAME)
	Assert(err)
	if found {
		glog.Infof("Deleting the grouy: %s, as it already exists", GROUP_NAME)
		id, err := client.DeleteGroup(GROUP_NAME)
		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(GROUP_NAME)
	group.App(frontend).App(redis).App(mysql)

	Assert(client.CreateGroup(group))
	glog.Infof("Successfully created the group: %s", group.ID)

	glog.Infof("Updating the group paramaters")
	frontend.Count(4)

	id, err := client.UpdateGroup(GROUP_NAME, group)
	Assert(err)
	glog.Infof("Successfully updated the group: %s, version: %s", group.ID, id.DeploymentID)
	Assert(client.WaitOnGroup(GROUP_NAME, 500*time.Second))
}