本文整理汇总了Golang中github.com/wercker/wercker/core.Session类的典型用法代码示例。如果您正苦于以下问题:Golang Session类的具体用法?Golang Session怎么用?Golang Session使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Session类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Execute
// Execute a shell and give it to the user
func (s *ShellStep) Execute(ctx context.Context, sess *core.Session) (int, error) {
// cheating to get containerID
// TODO(termie): we should deal with this eventually
dt := sess.Transport().(*DockerTransport)
containerID := dt.containerID
client, err := NewDockerClient(s.dockerOptions)
if err != nil {
return -1, err
}
code := s.env.Export()
code = append(code, "cd $WERCKER_SOURCE_DIR")
code = append(code, s.Code)
err = client.AttachInteractive(containerID, s.Cmd, code)
if err != nil {
return -1, err
}
return 0, nil
}
示例2: Execute
// Execute commits the current container and pushes it to the configured
// registry
func (s *DockerPushStep) Execute(ctx context.Context, sess *core.Session) (int, error) {
// TODO(termie): could probably re-use the tansport's client
client, err := NewDockerClient(s.dockerOptions)
if err != nil {
return 1, err
}
e, err := core.EmitterFromContext(ctx)
if err != nil {
return 1, err
}
s.logger.WithFields(util.LogFields{
"Registry": s.registry,
"Repository": s.repository,
"Tags": s.tags,
"Message": s.message,
}).Debug("Push to registry")
// This is clearly only relevant to docker so we're going to dig into the
// transport internals a little bit to get the container ID
dt := sess.Transport().(*DockerTransport)
containerID := dt.containerID
auth := docker.AuthConfiguration{
Username: s.username,
Password: s.password,
Email: s.email,
ServerAddress: s.authServer,
}
if !s.dockerOptions.DockerLocal {
checkOpts := CheckAccessOptions{
Auth: auth,
Access: "write",
Repository: s.repository,
Registry: s.registry,
}
check, err := client.CheckAccess(checkOpts)
if err != nil {
s.logger.Errorln("Error during check access", err)
return -1, err
}
if !check {
s.logger.Errorln("Not allowed to interact with this repository:", s.repository)
return -1, fmt.Errorf("Not allowed to interact with this repository: %s", s.repository)
}
}
s.logger.Debugln("Init env:", s.data)
config := docker.Config{
Cmd: s.cmd,
Entrypoint: s.entrypoint,
WorkingDir: s.workingDir,
User: s.user,
Env: s.env,
StopSignal: s.stopSignal,
Labels: s.labels,
ExposedPorts: s.ports,
Volumes: s.volumes,
}
if len(s.tags) == 0 {
s.tags = []string{"latest"}
}
commitOpts := docker.CommitContainerOptions{
Container: containerID,
Repository: s.repository,
Author: s.author,
Message: s.message,
Run: &config,
Tag: s.tags[0],
}
s.logger.Debugln("Commit container:", containerID)
i, err := client.CommitContainer(commitOpts)
if err != nil {
return -1, err
}
s.logger.WithField("Image", i).Debug("Commit completed")
return s.tagAndPush(i.ID, e, client, auth)
}
示例3: Execute
// Execute does the actual export and upload of the container
func (s *StoreContainerStep) Execute(ctx context.Context, sess *core.Session) (int, error) {
e, err := core.EmitterFromContext(ctx)
if err != nil {
return -1, err
}
// TODO(termie): could probably re-use the tansport's client
client, err := NewDockerClient(s.dockerOptions)
if err != nil {
return -1, err
}
// This is clearly only relevant to docker so we're going to dig into the
// transport internals a little bit to get the container ID
dt := sess.Transport().(*DockerTransport)
containerID := dt.containerID
repoName := s.DockerRepo()
tag := s.DockerTag()
message := s.DockerMessage()
commitOpts := docker.CommitContainerOptions{
Container: containerID,
Repository: repoName,
Tag: tag,
Author: "wercker",
Message: message,
}
s.logger.Debugln("Commit container:", containerID)
i, err := client.CommitContainer(commitOpts)
if err != nil {
return -1, err
}
s.logger.WithField("Image", i).Debug("Commit completed")
e.Emit(core.Logs, &core.LogsArgs{
Logs: "Exporting container\n",
})
file, err := ioutil.TempFile(s.options.BuildPath(), "export-image-")
if err != nil {
s.logger.WithField("Error", err).Error("Unable to create temporary file")
return -1, err
}
hash := sha256.New()
w := snappystream.NewWriter(io.MultiWriter(file, hash))
exportImageOptions := docker.ExportImageOptions{
Name: repoName,
OutputStream: w,
}
err = client.ExportImage(exportImageOptions)
if err != nil {
s.logger.WithField("Error", err).Error("Unable to export image")
return -1, err
}
// Copy is done now, so close temporary file and set the calculatedHash
file.Close()
calculatedHash := hex.EncodeToString(hash.Sum(nil))
s.logger.WithFields(util.LogFields{
"SHA256": calculatedHash,
"TemporaryLocation": file.Name(),
}).Println("Export image successful")
key := core.GenerateBaseKey(s.options)
key = fmt.Sprintf("%s/%s", key, "docker.tar.sz")
s.artifact = &core.Artifact{
HostPath: file.Name(),
Key: key,
Bucket: s.options.S3Bucket,
ContentType: "application/x-snappy-framed",
Meta: map[string]*string{
"Sha256": &calculatedHash,
},
}
return 0, nil
}
示例4: Execute
// Execute the scratch-n-push
func (s *DockerScratchPushStep) Execute(ctx context.Context, sess *core.Session) (int, error) {
// This is clearly only relevant to docker so we're going to dig into the
// transport internals a little bit to get the container ID
dt := sess.Transport().(*DockerTransport)
containerID := dt.containerID
_, err := s.CollectArtifact(containerID)
if err != nil {
return -1, err
}
// layer.tar has an extra folder in it so we have to strip it :/
artifactReader, err := os.Open(s.options.HostPath("layer.tar"))
if err != nil {
return -1, err
}
defer artifactReader.Close()
layerFile, err := os.OpenFile(s.options.HostPath("real_layer.tar"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return -1, err
}
defer layerFile.Close()
dgst := digest.Canonical.New()
mwriter := io.MultiWriter(layerFile, dgst.Hash())
tr := tar.NewReader(artifactReader)
tw := tar.NewWriter(mwriter)
for {
hdr, err := tr.Next()
if err == io.EOF {
// finished the tarball
break
}
if err != nil {
return -1, err
}
// Skip the base dir
if hdr.Name == "./" {
continue
}
if strings.HasPrefix(hdr.Name, "output/") {
hdr.Name = hdr.Name[len("output/"):]
} else if strings.HasPrefix(hdr.Name, "source/") {
hdr.Name = hdr.Name[len("source/"):]
}
if len(hdr.Name) == 0 {
continue
}
tw.WriteHeader(hdr)
_, err = io.Copy(tw, tr)
if err != nil {
return -1, err
}
}
digest := dgst.Digest()
config := &container.Config{
Cmd: s.cmd,
Entrypoint: s.entrypoint,
Hostname: containerID[:16],
WorkingDir: s.workingDir,
Volumes: s.volumes,
ExposedPorts: tranformPorts(s.ports),
}
// Make the JSON file we need
t := time.Now()
base := image.V1Image{
Architecture: "amd64",
Container: containerID,
ContainerConfig: container.Config{
Hostname: containerID[:16],
},
DockerVersion: "1.10",
Created: t,
OS: "linux",
Config: config,
}
imageJSON := image.Image{
V1Image: base,
History: []image.History{image.History{Created: t}},
RootFS: &image.RootFS{
Type: "layers",
DiffIDs: []layer.DiffID{layer.DiffID(digest)},
},
}
js, err := imageJSON.MarshalJSON()
if err != nil {
//.........这里部分代码省略.........
示例5: Execute
// Execute runs a command and optionally reloads it
func (s *WatchStep) Execute(ctx context.Context, sess *core.Session) (int, error) {
e, err := core.EmitterFromContext(ctx)
if err != nil {
return -1, err
}
// TODO(termie): PACKAGING make this a feature of session and remove
// the calls into its struct
// Start watching our stdout
stopListening := make(chan struct{})
defer func() { stopListening <- struct{}{} }()
go func() {
for {
select {
case line := <-sess.Recv():
e.Emit(core.Logs, &core.LogsArgs{
// Hidden: sess.logsHidden,
Logs: line,
})
// We need to make sure we stop eating the stdout from the container
// promiscuously when we finish out step
case <-stopListening:
return
}
}
}()
// cheating to get containerID
// TODO(termie): we should deal with this eventually
dt := sess.Transport().(*DockerTransport)
containerID := dt.containerID
// Set up a signal handler to end our step.
finishedStep := make(chan struct{})
stopWatchHandler := &util.SignalHandler{
ID: "stop-watch",
// Signal our stuff to stop and finish the step, return false to
// signify that we've handled the signal and don't process further
F: func() bool {
s.logger.Println("Keyboard interrupt detected, finishing step")
finishedStep <- struct{}{}
return false
},
}
util.GlobalSigint().Add(stopWatchHandler)
// NOTE(termie): I think the only way to exit this code is via this
// signal handler and the signal monkey removes handlers
// after it processes them, so this may be superfluous
defer util.GlobalSigint().Remove(stopWatchHandler)
// If we're not going to reload just run the thing once, synchronously
if !s.reload {
err := sess.Send(ctx, false, "set +e", s.Code)
if err != nil {
return 0, err
}
<-finishedStep
// ignoring errors
s.killProcesses(containerID, "INT")
return 0, nil
}
f := &util.Formatter{s.options.GlobalOptions.ShowColors}
s.logger.Info(f.Info("Reloading on file changes"))
doCmd := func() {
err := sess.Send(ctx, false, "set +e", s.Code)
if err != nil {
s.logger.Errorln(err)
return
}
open, err := exposedPortMaps(s.dockerOptions.DockerHost, s.options.PublishPorts)
if err != nil {
s.logger.Warnf(f.Info("There was a problem parsing your docker host."), err)
return
}
for _, uri := range open {
s.logger.Infof(f.Info("Forwarding %s to %s on the container."), uri.HostURI, uri.ContainerPort)
}
}
// Otherwise set up a watcher and do some magic
watcher, err := s.watch(s.options.ProjectPath)
if err != nil {
return -1, err
}
debounce := util.NewDebouncer(2 * time.Second)
done := make(chan struct{})
go func() {
for {
select {
case event := <-watcher.Events:
s.logger.Debugln("fsnotify event", event.String())
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create || event.Op&fsnotify.Remove == fsnotify.Remove {
if !strings.HasPrefix(filepath.Base(event.Name), ".") {
s.logger.Debug(f.Info("Modified file", event.Name))
debounce.Trigger()
}
}
case <-debounce.C:
//.........这里部分代码省略.........