func TestFormatUUID4(t *testing.T) { first4 := uuid.NewRandom() other4 := uuid.NewRandom() uuid4 := UUID4(first4.String()) str := string(other4.String()) b := []byte(str) bj := []byte("\"" + str + "\"") err := uuid4.UnmarshalText(b) assert.NoError(t, err) assert.EqualValues(t, UUID4(other4.String()), string(b)) b, err = uuid4.MarshalText() assert.NoError(t, err) assert.Equal(t, []byte(other4.String()), b) err = uuid4.UnmarshalJSON(bj) assert.NoError(t, err) assert.EqualValues(t, UUID4(str), string(b)) b, err = uuid4.MarshalJSON() assert.NoError(t, err) assert.Equal(t, bj, b) testValid(t, "uuid4", str) testInvalid(t, "uuid4", "not-a-uuid") }
func getSessionSecrets(filename string) ([]string, error) { // Build secrets list secrets := []string{} if len(filename) != 0 { sessionSecrets, err := latest.ReadSessionSecrets(filename) if err != nil { return nil, fmt.Errorf("error reading sessionSecretsFile %s: %v", filename, err) } if len(sessionSecrets.Secrets) == 0 { return nil, fmt.Errorf("sessionSecretsFile %s contained no secrets", filename) } for _, s := range sessionSecrets.Secrets { secrets = append(secrets, s.Authentication) secrets = append(secrets, s.Encryption) } } else { // Generate random signing and encryption secrets if none are specified in config secrets = append(secrets, fmt.Sprintf("%x", md5.Sum([]byte(uuid.NewRandom().String())))) secrets = append(secrets, fmt.Sprintf("%x", md5.Sum([]byte(uuid.NewRandom().String())))) } return secrets, nil }
func TestValidateXattrSupport(t *testing.T) { defer heketitests.Patch(&Setxattr, tests.MockSetxattr).Restore() defer heketitests.Patch(&Getxattr, tests.MockGetxattr).Restore() defer heketitests.Patch(&Removexattr, tests.MockRemovexattr).Restore() tests.Assert(t, ValidateXattrSupport("/tmp/b1", "localhost", uuid.NewRandom(), true) == nil) // Some negative tests var xattr_err error baderror := errors.New("Bad") xattr_err = baderror // Now check what happens when setxattr fails defer heketitests.Patch(&Setxattr, func(path string, attr string, data []byte, flags int) (err error) { return xattr_err }).Restore() tests.Assert(t, ValidateXattrSupport("/tmp/b1", "localhost", uuid.NewRandom(), true) == baderror) // Now check what happens when getxattr fails defer heketitests.Patch(&Getxattr, func(path string, attr string, dest []byte) (sz int, err error) { return 0, xattr_err }).Restore() tests.Assert(t, ValidateXattrSupport("/tmp/b1", "localhost", uuid.NewRandom(), true) == baderror) // Now check what happens when removexattr fails defer heketitests.Patch(&Removexattr, func(path string, attr string) (err error) { return xattr_err }).Restore() tests.Assert(t, ValidateXattrSupport("/tmp/b1", "localhost", uuid.NewRandom(), true) == baderror) }
// GenerateAccessToken generates base64-encoded UUID access and refresh tokens func (a *AccessTokenGenDefault) GenerateAccessToken(c context.Context, data *AccessData, generaterefresh bool) (accesstoken string, refreshtoken string, err error) { token := uuid.NewRandom() accesstoken = removePadding(base64.URLEncoding.EncodeToString([]byte(token))) if generaterefresh { rtoken := uuid.NewRandom() refreshtoken = removePadding(base64.URLEncoding.EncodeToString([]byte(rtoken))) } return }
func (s *btrfsMigrationSourceDriver) SendWhileRunning(conn *websocket.Conn, op *operation) error { if s.container.IsSnapshot() { tmpPath := containerPath(fmt.Sprintf("%s/.migration-send-%s", s.container.Name(), uuid.NewRandom().String()), true) err := os.MkdirAll(tmpPath, 0700) if err != nil { return err } btrfsPath := fmt.Sprintf("%s/.root", tmpPath) if err := s.btrfs.subvolSnapshot(s.container.Path(), btrfsPath, true); err != nil { return err } defer s.btrfs.subvolDelete(btrfsPath) wrapper := StorageProgressReader(op, "fs_progress", s.container.Name()) return s.send(conn, btrfsPath, "", wrapper) } for i, snap := range s.snapshots { prev := "" if i > 0 { prev = s.snapshots[i-1].Path() } wrapper := StorageProgressReader(op, "fs_progress", snap.Name()) if err := s.send(conn, snap.Path(), prev, wrapper); err != nil { return err } } /* We can't send running fses, so let's snapshot the fs and send * the snapshot. */ tmpPath := containerPath(fmt.Sprintf("%s/.migration-send-%s", s.container.Name(), uuid.NewRandom().String()), true) err := os.MkdirAll(tmpPath, 0700) if err != nil { return err } s.runningSnapName = fmt.Sprintf("%s/.root", tmpPath) if err := s.btrfs.subvolSnapshot(s.container.Path(), s.runningSnapName, true); err != nil { return err } btrfsParent := "" if len(s.btrfsSnapshotNames) > 0 { btrfsParent = s.btrfsSnapshotNames[len(s.btrfsSnapshotNames)-1] } wrapper := StorageProgressReader(op, "fs_progress", s.container.Name()) return s.send(conn, s.runningSnapName, btrfsParent, wrapper) }
// WithAudit decorates a http.Handler with audit logging information for all the // requests coming to the server. Each audit log contains two entries: // 1. the request line containing: // - unique id allowing to match the response line (see 2) // - source ip of the request // - HTTP method being invoked // - original user invoking the operation // - impersonated user for the operation // - namespace of the request or <none> // - uri is the full URI as requested // 2. the response line containing: // - the unique id from 1 // - response code func WithAudit(handler http.Handler, attributeGetter apiserver.RequestAttributeGetter, out io.Writer) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { attribs := attributeGetter.GetAttribs(req) asuser := req.Header.Get(authenticationapi.ImpersonateUserHeader) if len(asuser) == 0 { asuser = "******" } asgroups := "<lookup>" requestedGroups := req.Header[authenticationapi.ImpersonateGroupHeader] if len(requestedGroups) > 0 { quotedGroups := make([]string, len(requestedGroups)) for i, group := range requestedGroups { quotedGroups[i] = fmt.Sprintf("%q", group) } asgroups = strings.Join(quotedGroups, ", ") } namespace := attribs.GetNamespace() if len(namespace) == 0 { namespace = "<none>" } id := uuid.NewRandom().String() line := fmt.Sprintf("%s AUDIT: id=%q ip=%q method=%q user=%q as=%q asgroups=%q namespace=%q uri=%q\n", time.Now().Format(time.RFC3339Nano), id, utilnet.GetClientIP(req), req.Method, attribs.GetUser().GetName(), asuser, asgroups, namespace, req.URL) if _, err := fmt.Fprint(out, line); err != nil { glog.Errorf("Unable to write audit log: %s, the error is: %v", line, err) } respWriter := decorateResponseWriter(w, out, id) handler.ServeHTTP(respWriter, req) }) }
// WithAudit decorates a http.Handler with audit logging information for all the // requests coming to the server. If out is nil, no decoration takes place. // Each audit log contains two entries: // 1. the request line containing: // - unique id allowing to match the response line (see 2) // - source ip of the request // - HTTP method being invoked // - original user invoking the operation // - impersonated user for the operation // - namespace of the request or <none> // - uri is the full URI as requested // 2. the response line containing: // - the unique id from 1 // - response code func WithAudit(handler http.Handler, attributeGetter RequestAttributeGetter, out io.Writer) http.Handler { if out == nil { return handler } return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { attribs, err := attributeGetter.GetAttribs(req) if err != nil { internalError(w, req, err) return } asuser := req.Header.Get("Impersonate-User") if len(asuser) == 0 { asuser = "******" } namespace := attribs.GetNamespace() if len(namespace) == 0 { namespace = "<none>" } id := uuid.NewRandom().String() line := fmt.Sprintf("%s AUDIT: id=%q ip=%q method=%q user=%q as=%q namespace=%q uri=%q\n", time.Now().Format(time.RFC3339Nano), id, utilnet.GetClientIP(req), req.Method, attribs.GetUser().GetName(), asuser, namespace, req.URL) if _, err := fmt.Fprint(out, line); err != nil { glog.Errorf("Unable to write audit log: %s, the error is: %v", line, err) } respWriter := decorateResponseWriter(w, out, id) handler.ServeHTTP(respWriter, req) }) }
func (s *OptionsSuite) TestGuessAuthToken() { tmpFile, err := ioutil.TempFile("", "test-auth-token") s.Nil(err) token := uuid.NewRandom().String() _, err = tmpFile.Write([]byte(token)) s.Nil(err) tokenStore := tmpFile.Name() defer os.Remove(tokenStore) defer tmpFile.Close() args := []string{ "wercker", "--auth-token-store", tokenStore, "test", } test := func(c *cli.Context) { opts, err := core.NewGlobalOptions(util.NewCLISettings(c), emptyEnv()) s.Nil(err) s.Equal(token, opts.AuthToken) } run(s, globalFlags, emptyFlags, test, args) }
func (db *DB) CreateRetentionPolicy(expiry uint) (uuid.UUID, error) { id := uuid.NewRandom() return id, db.Exec( `INSERT INTO retention (uuid, expiry) VALUES (?, ?)`, id.String(), expiry, ) }
func (p *ProcessorPool) runProcessor(queue JobQueue) error { processorUUID := uuid.NewRandom() ctx := context.FromProcessor(p.Context, processorUUID.String()) jobsChan, err := queue.Jobs(ctx) if err != nil { context.LoggerFromContext(p.Context).WithField("err", err).Error("couldn't create jobs channel") return err } proc, err := NewProcessor(ctx, p.Hostname, jobsChan, p.Provider, p.Generator, p.Canceller, p.HardTimeout, p.LogTimeout) if err != nil { context.LoggerFromContext(p.Context).WithField("err", err).Error("couldn't create processor") return err } proc.SkipShutdownOnLogTimeout = p.SkipShutdownOnLogTimeout p.processorsLock.Lock() p.processors = append(p.processors, proc) p.processorsLock.Unlock() proc.Run() return nil }
// NewDeployOptions constructor func NewDeployOptions(c util.Settings, e *util.Environment) (*PipelineOptions, error) { pipelineOpts, err := NewPipelineOptions(c, e) if err != nil { return nil, err } // default to last build output if none defined target, _ := c.String("target") if target == "" { found, err := util.Exists("./.wercker/latest/output") if err == nil && found { util.RootLogger().Println("No target specified, using recent build output.") pipelineOpts.ProjectPath, _ = filepath.Abs("./.wercker/latest/output") } } // if the deploy target path does not have a wercker.yml, use the current one werckerYml, _ := c.String("wercker-yml") if werckerYml == "" { found, _ := util.Exists(filepath.Join(pipelineOpts.ProjectPath, "wercker.yml")) if !found { pipelineOpts.WerckerYml = "./wercker.yml" } } if pipelineOpts.RunID == "" { pipelineOpts.RunID = uuid.NewRandom().String() } return pipelineOpts, nil }
func (f *StreamAggregatorFilter) committer(fr FilterRunner, h PluginHelper, wg *sync.WaitGroup) { initBatch := make([]byte, 0, 10000) f.backChan <- initBatch var ( tag string outBatch []byte ) tag = f.StreamAggregatorTag for outBatch = range f.batchChan { pack, e := h.PipelinePack(f.msgLoopCount) if e != nil { fr.LogError(e) break } tagField, _ := message.NewField("StreamAggregatorTag", tag, "") pack.Message.AddField(tagField) pack.Message.SetUuid(uuid.NewRandom()) if f.OutputType == "json" { jsonStr := strings.TrimRight(string(outBatch), ",") pack.Message.SetPayload("[" + jsonStr + "]") } else { pack.Message.SetPayload(string(outBatch)) } fr.Inject(pack) outBatch = outBatch[:0] f.backChan <- outBatch } wg.Done() }
func eventsSocket(r *http.Request, w http.ResponseWriter) error { listener := eventListener{} typeStr := r.FormValue("type") if typeStr == "" { typeStr = "logging,operation" } c, err := shared.WebsocketUpgrader.Upgrade(w, r, nil) if err != nil { return err } listener.active = make(chan bool, 1) listener.connection = c listener.id = uuid.NewRandom().String() listener.messageTypes = strings.Split(typeStr, ",") eventsLock.Lock() eventListeners[listener.id] = &listener eventsLock.Unlock() shared.Debugf("New events listener: %s", listener.id) <-listener.active return nil }
func getRandVolume() *Volinfo { v := NewVolinfoFunc() v.ID = uuid.NewRandom() v.Name = fmt.Sprintf("volume-%d", atomic.AddUint64(&volCount, 1)) v.Type = DistReplicate brickCount := uint64(rand.Intn(256) + 1) for i := uint64(1); i <= brickCount; i++ { //v.Bricks = append(v.Bricks, fmt.Sprintf("host:/brick-%d", i)) v.Bricks[i].Hostname = "Host" v.Bricks[i].Path = fmt.Sprintf("/brick-%d", i) v.Bricks[i].ID = v.ID } v.DistCount = uint64(rand.Intn(256) + 1) v.ReplicaCount = uint16(rand.Intn(10)) v.StripeCount = uint16(rand.Intn(10)) v.DisperseCount = uint16(rand.Intn(10)) v.RedundancyCount = uint16(rand.Intn(10)) v.Status = VolCreated v.Checksum = uint64(rand.Uint32()) v.Version = 1 return v }
func (img *Image) buildPodManifest(exec []string) *schema.PodManifest { bpm := schema.BlankPodManifest() // Figure out working path that doesn't exist in the image's rootfs workDir := ".jetpack.build." for { if _, err := os.Stat(img.getRootfs().Path(workDir)); err != nil { if os.IsNotExist(err) { break } panic(err) } workDir = fmt.Sprintf(".jetpack.build.%v", uuid.NewRandom()) } bprta := img.RuntimeApp() bprta.Name.Set("jetpack/build") bprta.App = &types.App{ Exec: exec, WorkingDirectory: "/" + workDir, User: "******", Group: "0", } bpm.Apps = append(bpm.Apps, bprta) // This is needed by freebsd-update at least, should be okay to // allow this in builders. bpm.Annotations.Set("jetpack/jail.conf/allow.chflags", "true") bpm.Annotations.Set("jetpack/jail.conf/securelevel", "0") return bpm }
func newNotice(config *Configuration, err Error, extra ...interface{}) *Notice { notice := Notice{ APIKey: config.APIKey, Error: err, Token: uuid.NewRandom().String(), ErrorMessage: err.Message, ErrorClass: err.Class, Env: config.Env, Hostname: config.Hostname, Backtrace: composeStack(err.Stack, config.Root), ProjectRoot: config.Root, Context: Context{}, } for _, thing := range extra { switch t := thing.(type) { case Context: notice.setContext(t) case Params: notice.Params = t case CGIData: notice.CGIData = t case url.URL: notice.URL = t.String() } } return ¬ice }
func AddComment(w rest.ResponseWriter, req *rest.Request) { requesterID := GetRequesterID(req) var comment types.Comment err := req.DecodeJsonPayload(&comment) if err != nil { log.Println(err) rest.Error(w, err.Error(), http.StatusInternalServerError) return } comment.Time = time.Now().UTC() comment.PostID = req.PathParam("pid") comment.CreatorID = requesterID comment.ID = base64.RawURLEncoding.EncodeToString(uuid.NewRandom()) err = comment.Validate() if err != nil { log.Println(err) rest.Error(w, err.Error(), http.StatusInternalServerError) return } err = transactions.AddComment(requesterID, &comment) if err != nil { log.Println(err) rest.Error(w, err.Error(), http.StatusInternalServerError) return } insertedComment, err := transactions.GetComment(requesterID, comment.ID) if err != nil { log.Println(err) rest.Error(w, err.Error(), http.StatusInternalServerError) return } w.WriteJson(insertedComment) w.WriteHeader(http.StatusOK) }
func (f *ZlibFilter) committer(fr FilterRunner, h PluginHelper, wg *sync.WaitGroup) { initBatch := make([]byte, 0, 10000) f.backChan <- initBatch var ( tag string //ok bool outBatch []byte ) tag = f.ZlibTag for outBatch = range f.batchChan { pack, e := h.PipelinePack(f.msgLoopCount) if e != nil { fr.LogError(e) break } var b bytes.Buffer w := zlib.NewWriter(&b) w.Write(outBatch) w.Close() tagField, _ := message.NewField("ZlibTag", tag, "") pack.Message.AddField(tagField) pack.Message.SetUuid(uuid.NewRandom()) pack.Message.SetPayload(b.String()) fr.Inject(pack) outBatch = outBatch[:0] f.backChan <- outBatch } wg.Done() }
// Create and return a new Node. channels for sending and receiving messages are setup. // And two goroutines are spawned Send and Receive to handle incoming and outgoing messages. func NewNode(conn net.Conn) *Node { n := new(Node) n.Client = NewClient(conn) n.ID = uuid.NewRandom() n.Name = n.ID.String() n.State = UNKNOWN n.fsm = fsm.NewFSM( n.State.S(), fsm.Events{ {Name: "offline", Src: StateList(ONLINE, READY, ASSIGNING, WORKING), Dst: OFFLINE.S()}, {Name: "online", Src: StateList(UNKNOWN, READY, OFFLINE), Dst: ONLINE.S()}, {Name: "ready", Src: StateList(ONLINE, WORKING, ASSIGNING), Dst: READY.S()}, {Name: "assign", Src: StateList(READY), Dst: ASSIGNING.S()}, {Name: "work", Src: StateList(ASSIGNING), Dst: WORKING.S()}, {Name: "service", Src: StateList(UNKNOWN, ONLINE, OFFLINE), Dst: SERVICE.S()}, }, fsm.Callbacks{ "after_event": func(e *fsm.Event) { n.afterEvent(e) }, "before_offline": func(e *fsm.Event) { n.beforeOffline(e) }, OFFLINE.S(): func(e *fsm.Event) { n.offlineNode(e) }, }, ) n.Send = make(chan *proto_msg.KamajiMessage) n.Receive = make(chan *proto_msg.KamajiMessage) n.done = make(chan bool) n.waitGroup = &sync.WaitGroup{} if n.Conn != nil { go n.messageTransmitter() go n.messageReciever() } return n }
// Create a new Command instance and return it. func NewCommand(name string, task *Task) *Command { c := new(Command) c.ID = uuid.NewRandom() c.Name = name c.State = UNKNOWN c.Completion = 0.0 c.created = time.Now() c.priority = 0 c.Task = task if task != nil { task.Commands = append(task.Commands, c) } c.FSM = fsm.NewFSM( c.State.S(), fsm.Events{ {Name: "ready", Src: StateList(UNKNOWN, STOPPED, ASSIGNING), Dst: READY.S()}, {Name: "assign", Src: StateList(READY), Dst: ASSIGNING.S()}, {Name: "start", Src: StateList(UNKNOWN, READY, ASSIGNING, STOPPED), Dst: WORKING.S()}, {Name: "restart", Src: StateList(DONE), Dst: WORKING.S()}, {Name: "finish", Src: StateList(WORKING), Dst: DONE.S()}, {Name: "stop", Src: StateList(WORKING), Dst: STOPPED.S()}, }, fsm.Callbacks{ "after_event": func(e *fsm.Event) { c.afterEvent(e) }, DONE.S(): func(e *fsm.Event) { c.finishCommand(e) }, }, ) return c }
func (s *zfsMigrationSourceDriver) SendWhileRunning(conn *websocket.Conn) error { if s.container.IsSnapshot() { fields := strings.SplitN(s.container.Name(), shared.SnapshotDelimiter, 2) snapshotName := fmt.Sprintf("snapshot-%s", fields[1]) return s.send(conn, snapshotName, "") } lastSnap := "" for i, snap := range s.zfsSnapshotNames { prev := "" if i > 0 { prev = s.zfsSnapshotNames[i-1] } lastSnap = snap if err := s.send(conn, snap, prev); err != nil { return err } } s.runningSnapName = fmt.Sprintf("migration-send-%s", uuid.NewRandom().String()) if err := s.zfs.zfsSnapshotCreate(fmt.Sprintf("containers/%s", s.container.Name()), s.runningSnapName); err != nil { return err } if err := s.send(conn, s.runningSnapName, lastSnap); err != nil { return err } return nil }
func (localInstanceCreator *LocalInstanceCreator) Create(instanceID string) error { instanceCount, errs := localInstanceCreator.InstanceCount() if len(errs) > 0 { return errors.New("Failed to determine current instance count, view broker logs for details") } if instanceCount >= localInstanceCreator.RedisConfiguration.ServiceInstanceLimit { return brokerapi.ErrInstanceLimitMet } port, _ := localInstanceCreator.FindFreePort() instance := &Instance{ ID: instanceID, Port: port, Host: localInstanceCreator.RedisConfiguration.Host, Password: uuid.NewRandom().String(), } err := localInstanceCreator.Setup(instance) if err != nil { return err } err = localInstanceCreator.startLocalInstance(instance) if err != nil { return err } err = localInstanceCreator.Unlock(instance) if err != nil { return err } return nil }
// WithPasswordFromConfig retrieves the password from the configuration with path // as defined in constant PathJWTPassword func WithPasswordFromConfig(cr config.Getter) Option { pw, err := cr.String(config.Path(PathJWTPassword)) if config.NotKeyNotFoundError(err) { pw = string(uuid.NewRandom()) } return WithPassword([]byte(pw)) }
func NewStream(idx string, instruction StreamInstruction) (stream Stream) { stream.Identifier = idx stream.UUID = uuid.NewRandom().String() stream.Instruction = instruction stream.Playlist, _ = m3u8.NewMediaPlaylist(10000, 10000) // alloc a new one return }
func getTestMessage() *message.Message { hostname, _ := os.Hostname() field, _ := message.NewField("foo", "bar", "") msg := &message.Message{} msg.SetType("TEST") msg.SetTimestamp(5123456789) msg.SetPid(9283) msg.SetUuid(uuid.NewRandom()) msg.SetLogger("GoSpec") msg.SetSeverity(int32(6)) msg.SetEnvVersion("0.8") msg.SetPid(int32(os.Getpid())) msg.SetHostname(hostname) msg.AddField(field) var emptyByte []byte data := []byte("data") field1, _ := message.NewField("bytes", data, "") field2, _ := message.NewField("int", int64(999), "") field2.AddValue(int64(1024)) field3, _ := message.NewField("double", float64(99.9), "") field4, _ := message.NewField("bool", true, "") field5, _ := message.NewField("foo", "alternate", "") field6, _ := message.NewField("false", false, "") field7, _ := message.NewField("empty_bytes", emptyByte, "") msg.AddField(field1) msg.AddField(field2) msg.AddField(field3) msg.AddField(field4) msg.AddField(field5) msg.AddField(field6) msg.AddField(field7) return msg }
func (db *DB) CreateStore(plugin string, endpoint interface{}) (uuid.UUID, error) { id := uuid.NewRandom() return id, db.Exec( `INSERT INTO stores (uuid, plugin, endpoint) VALUES (?, ?, ?)`, id.String(), plugin, endpoint, ) }
// NewDockerPushStep is a special step for doing docker pushes func NewDockerPushStep(stepConfig *core.StepConfig, options *core.PipelineOptions, dockerOptions *DockerOptions) (*DockerPushStep, error) { name := "docker-push" displayName := "docker push" if stepConfig.Name != "" { displayName = stepConfig.Name } // Add a random number to the name to prevent collisions on disk stepSafeID := fmt.Sprintf("%s-%s", name, uuid.NewRandom().String()) baseStep := core.NewBaseStep(core.BaseStepOptions{ DisplayName: displayName, Env: &util.Environment{}, ID: name, Name: name, Owner: "wercker", SafeID: stepSafeID, Version: util.Version(), }) return &DockerPushStep{ BaseStep: baseStep, data: stepConfig.Data, logger: util.RootLogger().WithField("Logger", "DockerPushStep"), options: options, dockerOptions: dockerOptions, }, nil }
// RunAndAttach gives us a raw connection to a newly run container func (c *DockerClient) RunAndAttach(name string) error { hostConfig := &docker.HostConfig{} container, err := c.CreateContainer( docker.CreateContainerOptions{ Name: uuid.NewRandom().String(), Config: &docker.Config{ Image: name, Tty: true, OpenStdin: true, Cmd: []string{"/bin/bash"}, AttachStdin: true, AttachStdout: true, AttachStderr: true, // NetworkDisabled: b.networkDisabled, // Volumes: volumes, }, HostConfig: hostConfig, }) if err != nil { return err } c.StartContainer(container.ID, hostConfig) return c.AttachTerminal(container.ID) }
func init() { var buffer bytes.Buffer b := make([]byte, 16) // Get the current user name. osU, err := user.Current() user := "******" if err == nil { user = osU.Username } buffer.WriteString(user) // Create the constant to make build a unique ID. start := uint64(time.Now().UnixNano()) binary.PutUvarint(b, start) buffer.Write(b) pid := uint64(os.Getpid()) binary.PutUvarint(b, pid) buffer.Write(b) // Set the node. if !uuid.SetNodeID(buffer.Bytes()) { os.Exit(-1) } // Initialize the channel and blank node type. nextVal, tBlank = make(chan uuid.UUID, chanSize), Type("/_") go func() { for { nextVal <- uuid.NewRandom() } }() }
func main() { jobs := make(map[string]*Job) http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "pong") }) http.HandleFunc("/run", func(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { w.WriteHeader(405) return } b, _ := ioutil.ReadAll(r.Body) job := &Job{ Command: string(b), Running: true, } id := uuid.NewRandom() jobs[id.String()] = job go func() { output := sandbox.Run(sandbox.Options{ Image: "filefrog/sandbox", Command: string(job.Command), }) job.Output = output job.Running = false }() fmt.Fprintf(w, "%s", id) }) http.HandleFunc("/output/", func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { w.WriteHeader(405) return } id := strings.Split(r.URL.Path, "/")[2] if job, ok := jobs[id]; ok { if job.Running { w.WriteHeader(204) return } fmt.Fprintf(w, "%s", job.Output) return } w.WriteHeader(404) fmt.Fprintf(w, "uuid %s not found", id) }) http.Handle("/", http.FileServer(http.Dir("static"))) fmt.Printf("longshoreman up and running at http://localhost:8184\n") fmt.Printf(" go check your browser!\n") http.ListenAndServe(":8080", nil) }