Beispiel #1
0
func main() {
	flag.Parse()

	if *NAME == "" {
		log.Fatal("name required")
	}
	if *MEMORY == 0 {
		log.Fatal("memory required")
	}
	if *CPUS == 0 {
		log.Fatal("cpus required")
	}

	db, err := bolt.Open(*DB_PATH, 0600, nil)
	if err != nil {
		log.WithError(err).Fatal("failed to open database")
	}

	planrep := dal.NewBoltPlanrep(db)

	plan := &models.Plan{
		Name:     *NAME,
		Memory:   *MEMORY * 1024 * 1024,
		Cpus:     *CPUS,
		DiskSize: *DISK * 1024 * 1024 * 1024,
	}
	if err := planrep.Add(plan); err != nil {
		log.WithError(err).WithField("plan", plan).Fatal("failed to add ip address")
	}
}
Beispiel #2
0
func main() {
	flag.Parse()
	log.SetLevel(log.InfoLevel)

	router := mux.NewRouter().StrictSlash(true)

	renderer := render.New(render.Options{
		Extensions:    []string{".html"},
		IsDevelopment: true,
		Directory:     *TEMPLATE_PATH,
		Funcs: []template.FuncMap{
			template.FuncMap{
				"HasPrefix": strings.HasPrefix,
				"HumanizeDate": func(date time.Time) string {
					return date.Format("Mon Jan 2 15:04:05 -0700 MST 2006")
				},
				"Url": func(name string, params ...string) (string, error) {
					route := router.Get(name)
					if route == nil {
						return "", fmt.Errorf("route named %s not found", name)
					}
					url, err := route.URL(params...)
					if err != nil {
						return "", err
					}
					return url.Path, nil
				},
			},
		},
	})
	vmtpl, err := text_template.ParseFiles(*VM_TEMPLATE)
	if err != nil {
		log.WithError(err).WithField("filename", *VM_TEMPLATE).Fatal("failed to parse machine template")
	}
	machines, err := dal.NewLibvirtMachinerep("qemu:///system", vmtpl)
	if err != nil {
		log.WithError(err).Fatal("failed to initialize libvirt-kvm machines")
	}

	imagerep := dal.NewLocalfsImagerep(*IMAGES_PATH)

	metadb, err := bolt.Open(*METADB_PATH, 0600, nil)
	if err != nil {
		log.WithError(err).Fatal("failed to open metadata db")
	}

	planrep := dal.NewBoltPlanrep(metadb)
	ippool := dal.NewBoltIPPool(metadb)

	ctx := &vmango.Context{
		Render:   renderer,
		Router:   router,
		Machines: machines,
		Logger:   log.New(),
		Meta:     metadb,
		Images:   imagerep,
		IPPool:   ippool,
		Plans:    planrep,
	}

	router.Handle("/", vmango.NewHandler(ctx, handlers.Index)).Name("index")
	router.Handle("/machines/", vmango.NewHandler(ctx, handlers.MachineList)).Name("machine-list")
	router.Handle("/machines/add/", vmango.NewHandler(ctx, handlers.MachineAddForm)).Name("machine-add")
	router.Handle("/machines/{name:[^/]+}/", vmango.NewHandler(ctx, handlers.MachineDetail)).Name("machine-detail")
	router.Handle("/images/", vmango.NewHandler(ctx, handlers.ImageList)).Name("image-list")
	router.Handle("/ipaddress/", vmango.NewHandler(ctx, handlers.IPList)).Name("ip-list")
	router.Handle("/plans/", vmango.NewHandler(ctx, handlers.PlanList)).Name("plan-list")

	router.HandleFunc("/static{name:.*}", handlers.MakeStaticHandler(*STATIC_PATH)).Name("static")

	n := negroni.New()
	n.Use(negronilogrus.NewMiddleware())
	n.Use(negroni.NewRecovery())
	n.UseHandler(router)

	metaserv := cloudmeta.New()

	s := make(chan os.Signal, 1)
	signal.Notify(s, os.Interrupt)
	signal.Notify(s, syscall.SIGTERM)
	go func() {
		sig := <-s
		fmt.Println(sig, "received")
		signal.Reset(os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
		close(s)
		if err := metaserv.CleanupIPTables(*META_ADDR); err != nil {
			log.WithError(err).Warn("cannot remove metaserver iptables rules")
			os.Exit(1)
		}
		os.Exit(0)
	}()

	go func() {
		log.WithField("address", *META_ADDR).Info("starting cloud metadata server")
		log.Fatal(metaserv.ListenAndServe(*META_ADDR))
	}()

	log.WithField("address", *LISTEN_ADDR).Info("starting server")
	log.Fatal(http.ListenAndServe(*LISTEN_ADDR, n))
}