Esempio n. 1
0
func BasicAuthWrap(handler http.Handler, username, password string) http.Handler {
	opts := httpauth.AuthOptions{
		Realm:               "API Authentication",
		User:                username,
		Password:            password,
		UnauthorizedHandler: http.HandlerFunc(unauthorized),
	}
	return httpauth.BasicAuth(opts)(handler)
}
Esempio n. 2
0
File: app.go Progetto: yutopp/subako
func main() {
	defer func() {
		log.Println("Exit main")
	}()

	cwd, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}

	// read user config
	buffer, err := ioutil.ReadFile(path.Join(cwd, "config.yml"))
	if err != nil {
		log.Fatal(err)
	}
	var uConfig userConfig
	if err := yaml.Unmarshal(buffer, &uConfig); err != nil {
		log.Fatal(err)
	}

	//
	log.Printf("Port: %s", uConfig.Server.Port)
	log.Printf("Notification URL: %s", uConfig.Notification.Url)
	log.Printf("Cron Timing: %d:%d", uConfig.Cron.Hour, uConfig.Cron.Minute)
	log.Printf("ConfigSets IsRemote: %v", uConfig.ConfigSets.Remote)
	log.Printf("ConfigSets Path: %s", uConfig.ConfigSets.Path)
	if uConfig.Builder == nil {
		log.Fatal("builder section is not written in config")
	}
	log.Printf("PackagePrefix: %s", uConfig.Builder.PackagePrefix)
	log.Printf("InstallPrefix: %s", uConfig.Builder.InstallBasePrefix)
	if uConfig.ConfigSets.Remote {
		log.Printf("ConfigSets Repository: %s", uConfig.ConfigSets.Repository)
		log.Printf("ConfigSets RepoSecret: %s", uConfig.ConfigSets.RepoSecret)
	}

	// port
	flag.Set("bind", fmt.Sprintf(":%d", uConfig.Server.Port))

	// make storage dir
	storageDir := path.Join(cwd, "_storage")
	if !subako.Exists(storageDir) {
		if err := os.Mkdir(storageDir, 0755); err != nil {
			log.Fatal(err)
		}
	}

	// make config
	config := &subako.SubakoConfig{
		ProcConfigSetsConf: func() *subako.ProcConfigSetsConfig {
			path := func() string {
				if filepath.IsAbs(uConfig.ConfigSets.Path) {
					return uConfig.ConfigSets.Path
				} else {
					return path.Join(cwd, uConfig.ConfigSets.Path)
				}
			}()

			return &subako.ProcConfigSetsConfig{
				IsRemote:   uConfig.ConfigSets.Remote,
				BaseDir:    path,
				Repository: uConfig.ConfigSets.Repository,
			}
		}(),
		AvailablePackagesPath: path.Join(storageDir, "available_packages.json"),
		AptRepositoryBaseDir:  path.Join(storageDir, "apt_repository"),

		VirtualUsrDir:     path.Join(storageDir, "torigoya_usr"),
		TmpBaseDir:        path.Join(storageDir, "temp"),
		PackagesDir:       path.Join(storageDir, "packages"),
		PackagePrefix:     uConfig.Builder.PackagePrefix,
		InstallBasePrefix: uConfig.Builder.InstallBasePrefix,

		RunningTasksPath:   path.Join(storageDir, "running_tasks.json"),
		ProfilesHolderPath: path.Join(storageDir, "proc_profiles.json"),
		DataBasePath:       path.Join(storageDir, "db.sqlite"),
		NotificationConf: &subako.NotificationConfig{
			TargetUrl: uConfig.Notification.Url,
			Secret:    uConfig.Notification.Secret,
		},
		CronData: subako.Crontab{
			Hour:   uConfig.Cron.Hour,
			Minute: uConfig.Cron.Minute,
		},
		LogDir: "/tmp",
	}

	subakoCtx, err := subako.MakeSubakoContext(config)
	if err != nil {
		panic(err)
	}
	defer func() {
		if err := subakoCtx.Save(); err != nil {
			panic(err)
		}
	}()

	// add hooks for '__configs' (*special usage*)
	func() {
		if packageConfigsHookName == "" {
			return
		} // skip

		dummy_hook := &subako.Webhook{
			Target:   packageConfigsHookName,
			ProcName: "*For config update*",
		}
		hook, err := subakoCtx.Webhooks.GetByTargetOrCreate(packageConfigsHookName, dummy_hook)
		if err != nil {
			panic(err)
		}
		log.Println(hook)

		hook.Secret = uConfig.ConfigSets.RepoSecret
		if err := subakoCtx.Webhooks.Update(hook.ID, hook); err != nil {
			panic(err)
		}
	}()

	//
	gSubakoCtx = subakoCtx

	//
	authOpts := httpauth.AuthOptions{
		Realm:    "TorigoyaFactory",
		User:     uConfig.Auth.User,
		Password: uConfig.Auth.Password,
	}
	reqAuthMux := web.New()
	reqAuthMux.Use(httpauth.BasicAuth(authOpts))

	//
	pongo2.DefaultSet.SetBaseDirectory("views")

	goji.Get("/assets/*", http.StripPrefix("/assets/", http.FileServer(http.Dir("./public"))))
	goji.Get("/apt/*", http.StripPrefix("/apt/", http.FileServer(http.Dir(subakoCtx.AptRepoCtx.AptRepositoryBaseDir))))

	goji.Get("/", index)

	reqAuthMux.Get("/live_status/:id", liveStatus)
	reqAuthMux.Get("/abort_task/:id", abortTask)
	goji.Get("/status/:id", status)

	reqAuthMux.Get("/build/:name/:version", build)
	reqAuthMux.Get("/queue/:name/:version", queue)

	reqAuthMux.Get("/build/:name/:version/:dep_name/:dep_version", buildDep)
	reqAuthMux.Get("/queue/:name/:version/:dep_name/:dep_version", queueDep)

	goji.Get("/packages", showPackages)
	reqAuthMux.Get("/remove_package/:name/:version", removePackage)
	reqAuthMux.Get("/remove_package/:name/:version/:dep_name/:dep_version", removePackageDep)

	reqAuthMux.Get("/webhooks", webhooks)
	reqAuthMux.Post("/webhooks/append", webhooksAppend)
	reqAuthMux.Post("/webhooks/update/:id", webhooksUpdate)
	reqAuthMux.Post("/webhooks/delete/:id", webhooksDelete)
	goji.Post("/webhooks/fire/:name", webhookEvent)

	reqAuthMux.Get("/daily_tasks", dailyTasks)
	reqAuthMux.Post("/daily_tasks/append", dailyTasksAppend)
	reqAuthMux.Post("/daily_tasks/update/:id", dailyTasksUpdate)
	reqAuthMux.Post("/daily_tasks/delete/:id", dailyTasksDelete)

	reqAuthMux.Get("/update_proc_config_sets", updateProcConfigSets)
	reqAuthMux.Get("/regenerate_profiles", regenerateProfiles)

	reqAuthMux.Get("/system_logs", showMiniLogs)

	goji.Get("/information", showInfo)

	goji.Get("/api/profiles", showProfilesAPI)
	goji.Handle("/*", reqAuthMux)

	goji.Serve()
}