コード例 #1
0
ファイル: main.go プロジェクト: why2k/agent-test
func main() {
	flag.Parse()
	if Options.method_file == "" {
		glog.Errorf("You have not specified the methods file to import")
		os.Exit(1)
	}

	// step: open and read in the methods yaml
	contents, err := ioutil.ReadFile(Options.method_file)
	if err != nil {
		glog.Errorf("Failed to open|read the methods file: %s, error: %s", Options.method_file, err)
		os.Exit(1)
	}

	// step: unmarshal the yaml
	var methods []*RestMethod
	err = yaml.Unmarshal([]byte(contents), &methods)
	if err != nil {
		glog.Errorf("Failed to unmarshall the method yaml file: %s, error: %s", Options.method_file, err)
		os.Exit(1)
	}

	// step: construct a hash from the methods
	uris := make(map[string]*string, 0)
	for _, method := range methods {
		uris[fmt.Sprintf("%s:%s", method.Method, method.URI)] = &method.Content
	}

	http.HandleFunc("/", func(writer http.ResponseWriter, reader *http.Request) {
		key := fmt.Sprintf("%s:%s", reader.Method, reader.RequestURI)
		glog.V(4).Infof("Request: uri: %s, method: %s", reader.RequestURI, reader.Method)
		if content, found := uris[key]; found {
			glog.V(4).Infof("Content: %s", *content)
			writer.Header().Add("Content-Type", "application/json")
			writer.Write([]byte(*content))
		} else if !found {
			http.Error(writer, "not found", 404)
		}
	})
	glog.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", Options.port), nil))
}
コード例 #2
0
ファイル: main.go プロジェクト: why2k/agent-test
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)
	}
}
コード例 #3
0
ファイル: main.go プロジェクト: why2k/agent-test
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))
}