本文整理匯總了Golang中github.com/flynn/flynn/pkg/random.Hex函數的典型用法代碼示例。如果您正苦於以下問題:Golang Hex函數的具體用法?Golang Hex怎麽用?Golang Hex使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Hex函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: createDatabase
func createDatabase(db *postgres.DB, r render.Render) {
username, password, database := random.Hex(16), random.Hex(16), random.Hex(16)
if _, err := db.Exec(fmt.Sprintf(`CREATE USER "%s" WITH PASSWORD '%s'`, username, password)); err != nil {
log.Println(err)
r.JSON(500, struct{}{})
return
}
if _, err := db.Exec(fmt.Sprintf(`CREATE DATABASE "%s" WITH OWNER = "%s"`, database, username)); err != nil {
db.Exec(fmt.Sprintf(`DROP USER "%s"`, username))
log.Println(err)
r.JSON(500, struct{}{})
return
}
r.JSON(200, &resource{
ID: fmt.Sprintf("/databases/%s:%s", username, database),
Env: map[string]string{
"FLYNN_POSTGRES": serviceName,
"PGUSER": username,
"PGPASSWORD": password,
"PGDATABASE": database,
},
})
}
示例2: createDatabase
func (a *API) createDatabase(ctx context.Context, w http.ResponseWriter, req *http.Request) {
username, password, database := random.Hex(16), random.Hex(16), random.Hex(16)
if _, err := a.db.Exec(fmt.Sprintf("CREATE USER '%s'@'%%' IDENTIFIED BY '%s'", username, password)); err != nil {
httphelper.Error(w, err)
return
}
if _, err := a.db.Exec(fmt.Sprintf("CREATE DATABASE `%s`", database)); err != nil {
a.db.Exec(fmt.Sprintf("DROP USER '%s'", username))
httphelper.Error(w, err)
return
}
if _, err := a.db.Exec(fmt.Sprintf("GRANT ALL ON `%s`.* TO '%s'@'%%'", database, username)); err != nil {
a.db.Exec(fmt.Sprintf("DROP DATABASE `%s`", database))
a.db.Exec(fmt.Sprintf("DROP USER '%s'", username))
httphelper.Error(w, err)
return
}
url := fmt.Sprintf("mysql://%s:%[email protected]%s:3306/%s", username, password, serviceHost, database)
httphelper.JSON(w, 200, resource.Resource{
ID: fmt.Sprintf("/databases/%s:%s", username, database),
Env: map[string]string{
"FLYNN_MYSQL": serviceName,
"MYSQL_HOST": serviceHost,
"MYSQL_USER": username,
"MYSQL_PWD": password,
"MYSQL_DATABASE": database,
"DATABASE_URL": url,
},
})
}
示例3: createDatabase
func (p *pgAPI) createDatabase(ctx context.Context, w http.ResponseWriter, req *http.Request) {
username, password, database := random.Hex(16), random.Hex(16), random.Hex(16)
if err := p.db.Exec(fmt.Sprintf(`CREATE USER "%s" WITH PASSWORD '%s'`, username, password)); err != nil {
httphelper.Error(w, err)
return
}
if err := p.db.Exec(fmt.Sprintf(`CREATE DATABASE "%s"`, database)); err != nil {
p.db.Exec(fmt.Sprintf(`DROP USER "%s"`, username))
httphelper.Error(w, err)
return
}
if err := p.db.Exec(fmt.Sprintf(`GRANT ALL ON DATABASE "%s" TO "%s"`, database, username)); err != nil {
p.db.Exec(fmt.Sprintf(`DROP DATABASE "%s"`, database))
p.db.Exec(fmt.Sprintf(`DROP USER "%s"`, username))
httphelper.Error(w, err)
return
}
url := fmt.Sprintf("postgres://%s:%[email protected]%s:5432/%s", username, password, serviceHost, database)
httphelper.JSON(w, 200, resource.Resource{
ID: fmt.Sprintf("/databases/%s:%s", username, database),
Env: map[string]string{
"FLYNN_POSTGRES": serviceName,
"PGHOST": serviceHost,
"PGUSER": username,
"PGPASSWORD": password,
"PGDATABASE": database,
"DATABASE_URL": url,
},
})
}
示例4: TestKeyRotation
func (s *ControllerSuite) TestKeyRotation(t *c.C) {
cc := s.clusterConf(t)
oldKey := cc.Key
newKey := random.Hex(16)
// allow auth to API with old and new keys
set := flynn(t, "/", "-a", "controller", "env", "set", "-t", "web", fmt.Sprintf("AUTH_KEY=%s,%s", newKey, oldKey))
t.Assert(set, Succeeds)
// reconfigure components to use new key
for _, app := range []string{"gitreceive", "taffy", "dashboard"} {
set := flynn(t, "/", "-a", app, "env", "set", "CONTROLLER_KEY="+newKey)
t.Assert(set, Succeeds)
}
// write a new flynnrc
cc.Key = newKey
conf := &config.Config{}
err := conf.Add(cc, true)
t.Assert(err, c.IsNil)
err = conf.SaveTo(flynnrc)
t.Assert(err, c.IsNil)
// clear any cached configs
s.Helper.config = nil
s.Helper.controller = nil
// use new key for deployer+controller
set = flynn(t, "/", "-a", "controller", "env", "set", "AUTH_KEY="+newKey)
t.Assert(set, Succeeds)
// remove old key from API
set = flynn(t, "/", "-a", "controller", "env", "unset", "-t", "web", "AUTH_KEY")
t.Assert(set, Succeeds)
}
示例5: YesNoPrompt
func (s *httpInstaller) YesNoPrompt(msg string) bool {
prompt := &httpPrompt{
ID: random.Hex(16),
Type: "yes_no",
Message: msg,
resChan: make(chan *httpPrompt),
api: s.api,
}
prompt.api.InstallerPromptsMtx.Lock()
prompt.api.InstallerPrompts[prompt.ID] = prompt
prompt.api.InstallerPromptsMtx.Unlock()
s.sendEvent(&httpEvent{
Type: "prompt",
Prompt: prompt,
})
res := <-prompt.resChan
s.sendEvent(&httpEvent{
Type: "prompt",
Prompt: prompt,
})
return res.Yes
}
示例6: Run
func (a *GenRandomAction) Run(s *State) error {
if a.Length == 0 {
a.Length = 16
}
data := interpolate(s, a.Data)
if data == "" {
switch a.Encoding {
case "", "hex":
data = random.Hex(a.Length)
case "base64":
data = base64.StdEncoding.EncodeToString(random.Bytes(a.Length))
case "base64safe":
data = random.Base64(a.Length)
case "uuid":
data = random.UUID()
default:
return fmt.Errorf("bootstrap: unknown random type: %q", a.Encoding)
}
}
s.StepData[a.ID] = &RandomData{Data: data}
if a.ControllerKey {
s.SetControllerKey(data)
}
return nil
}
示例7: PromptInput
func (s *httpInstaller) PromptInput(msg string) string {
prompt := &httpPrompt{
ID: random.Hex(16),
Type: "input",
Message: msg,
resChan: make(chan *httpPrompt),
api: s.api,
}
s.api.InstallerPromptsMtx.Lock()
s.api.InstallerPrompts[prompt.ID] = prompt
s.api.InstallerPromptsMtx.Unlock()
s.sendEvent(&httpEvent{
Type: "prompt",
Prompt: prompt,
})
res := <-prompt.resChan
s.sendEvent(&httpEvent{
Type: "prompt",
Prompt: prompt,
})
return res.Input
}
示例8: createDatabase
func (a *API) createDatabase(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
// Ensure the cluster has been scaled up before attempting to create a database.
if err := a.scaleUp(); err != nil {
httphelper.Error(w, err)
return
}
session, err := mgo.DialWithInfo(&mgo.DialInfo{
Addrs: []string{net.JoinHostPort(serviceHost, "27017")},
Username: "flynn",
Password: os.Getenv("MONGO_PWD"),
Database: "admin",
})
if err != nil {
httphelper.Error(w, err)
return
}
defer session.Close()
username, password, database := random.Hex(16), random.Hex(16), random.Hex(16)
// Create a user
if err := session.DB(database).Run(bson.D{
{"createUser", username},
{"pwd", password},
{"roles", []bson.M{
{"role": "dbOwner", "db": database},
}},
}, nil); err != nil {
httphelper.Error(w, err)
return
}
url := fmt.Sprintf("mongodb://%s:%[email protected]%s:27017/%s", username, password, serviceHost, database)
httphelper.JSON(w, 200, resource.Resource{
ID: fmt.Sprintf("/databases/%s:%s", username, database),
Env: map[string]string{
"FLYNN_MONGO": serviceName,
"MONGO_HOST": serviceHost,
"MONGO_USER": username,
"MONGO_PWD": password,
"MONGO_DATABASE": database,
"DATABASE_URL": url,
},
})
}
示例9: YesNoPrompt
func (c *BaseCluster) YesNoPrompt(msg string) bool {
res := c.sendPrompt(&Prompt{
ID: random.Hex(16),
Type: "yes_no",
Message: msg,
resChan: make(chan *Prompt),
cluster: c,
})
return res.Yes
}
示例10: PromptInput
func (c *BaseCluster) PromptInput(msg string) string {
res := c.sendPrompt(&Prompt{
ID: random.Hex(16),
Type: "input",
Message: msg,
resChan: make(chan *Prompt),
cluster: c,
})
return res.Input
}
示例11: prompt
func (c *BaseCluster) prompt(typ, msg string) *Prompt {
if c.State != "starting" {
return &Prompt{}
}
res := c.sendPrompt(&Prompt{
ID: random.Hex(16),
Type: typ,
Message: msg,
resChan: make(chan *Prompt),
cluster: c,
})
return res
}
示例12: InstallHandler
func (api *httpAPI) InstallHandler(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
var input *jsonInput
if err := httphelper.DecodeJSON(req, &input); err != nil {
httphelper.Error(w, err)
return
}
api.InstallerStackMtx.Lock()
defer api.InstallerStackMtx.Unlock()
if len(api.InstallerStacks) > 0 {
httphelper.ObjectExistsError(w, "install already started")
return
}
var id = random.Hex(16)
var creds aws.CredentialsProvider
if input.Creds.AccessKeyID != "" && input.Creds.SecretAccessKey != "" {
creds = aws.Creds(input.Creds.AccessKeyID, input.Creds.SecretAccessKey, "")
} else {
var err error
creds, err = aws.EnvCreds()
if err != nil {
httphelper.ValidationError(w, "", err.Error())
return
}
}
s := &httpInstaller{
ID: id,
PromptOutChan: make(chan *httpPrompt),
PromptInChan: make(chan *httpPrompt),
logger: log.New(),
api: api,
}
s.Stack = &Stack{
Creds: creds,
Region: input.Region,
InstanceType: input.InstanceType,
NumInstances: input.NumInstances,
VpcCidr: input.VpcCidr,
SubnetCidr: input.SubnetCidr,
PromptInput: s.PromptInput,
YesNoPrompt: s.YesNoPrompt,
}
if err := s.Stack.RunAWS(); err != nil {
httphelper.Error(w, err)
return
}
api.InstallerStacks[id] = s
go s.handleEvents()
httphelper.JSON(w, 200, s)
}
示例13: Run
func (a *GenRandomAction) Run(s *State) error {
if a.Length == 0 {
a.Length = 16
}
data := interpolate(s, a.Data)
if data == "" {
data = random.Hex(a.Length)
}
s.StepData[a.ID] = &RandomData{Data: data}
if a.ControllerKey {
s.SetControllerKey(data)
}
return nil
}
示例14: createDatabase
func (p *pgAPI) createDatabase(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
username, password, database := random.Hex(16), random.Hex(16), random.Hex(16)
if err := p.db.Exec(fmt.Sprintf(`CREATE USER "%s" WITH PASSWORD '%s'`, username, password)); err != nil {
httphelper.Error(w, err)
return
}
if err := p.db.Exec(fmt.Sprintf(`CREATE DATABASE "%s" WITH OWNER = "%s"`, database, username)); err != nil {
p.db.Exec(fmt.Sprintf(`DROP USER "%s"`, username))
httphelper.Error(w, err)
return
}
httphelper.JSON(w, 200, resource.Resource{
ID: fmt.Sprintf("/databases/%s:%s", username, database),
Env: map[string]string{
"FLYNN_POSTGRES": serviceName,
"PGHOST": serviceHost,
"PGUSER": username,
"PGPASSWORD": password,
"PGDATABASE": database,
},
})
}
示例15: prompt
func (c *BaseCluster) prompt(typ PromptType, msg string) *Prompt {
state := c.getState()
if state != "starting" && state != "deleting" {
return &Prompt{}
}
res := c.sendPrompt(&Prompt{
ID: random.Hex(16),
Type: typ,
Message: msg,
resChan: make(chan *Prompt),
errChan: make(chan error),
cluster: c,
})
return res
}