本文整理汇总了Golang中github.com/square/p2/pkg/util.From函数的典型用法代码示例。如果您正苦于以下问题:Golang From函数的具体用法?Golang From怎么用?Golang From使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了From函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: FakeHoistLaunchableForDir
func FakeHoistLaunchableForDir(dirName string) (*Launchable, *runit.ServiceBuilder) {
tempDir, _ := ioutil.TempDir("", "fakeenv")
launchableInstallDir := util.From(runtime.Caller(0)).ExpandPath(dirName)
launchable := &Launchable{
Location: "testLaunchable.tar.gz",
Id: "testPod__testLaunchable",
RunAs: "testPod",
PodEnvDir: tempDir,
Fetcher: uri.DefaultFetcher,
RootDir: launchableInstallDir,
P2Exec: util.From(runtime.Caller(0)).ExpandPath("fake_p2-exec"),
}
curUser, err := user.Current()
if err == nil {
launchable.RunAs = curUser.Username
}
sbTemp, _ := ioutil.TempDir("", "fakesvdir")
sb := &runit.ServiceBuilder{
RunitRoot: sbTemp,
}
executables, _ := launchable.Executables(sb)
for _, exe := range executables {
os.MkdirAll(exe.Service.Path, 0644)
}
return launchable, sb
}
示例2: signBuild
func signBuild(artifactPath string) error {
sigLoc := fmt.Sprintf("%s.sig", artifactPath)
return exec.Command("gpg", "--no-default-keyring",
"--keyring", util.From(runtime.Caller(0)).ExpandPath("pubring.gpg"),
"--secret-keyring", util.From(runtime.Caller(0)).ExpandPath("secring.gpg"),
"-u", "p2universe",
"--out", sigLoc,
"--detach-sign", artifactPath).Run()
}
示例3: signManifest
func signManifest(manifestPath string, workdir string) (string, error) {
signedManifestPath := fmt.Sprintf("%s.asc", manifestPath)
return signedManifestPath,
exec.Command("gpg", "--no-default-keyring",
"--keyring", util.From(runtime.Caller(0)).ExpandPath("pubring.gpg"),
"--secret-keyring", util.From(runtime.Caller(0)).ExpandPath("secring.gpg"),
"-u", "p2universe",
"--output", signedManifestPath,
"--clearsign", manifestPath).Run()
}
示例4: signBuild
func signBuild(artifactPath string) error {
sigLoc := fmt.Sprintf("%s.sig", artifactPath)
output, err := exec.Command("gpg", "--no-default-keyring",
"--keyring", util.From(runtime.Caller(0)).ExpandPath("pubring.gpg"),
"--secret-keyring", util.From(runtime.Caller(0)).ExpandPath("secring.gpg"),
"-u", "p2universe",
"--out", sigLoc,
"--detach-sign", artifactPath).CombinedOutput()
if err != nil {
fmt.Println(string(output))
return err
}
return nil
}
示例5: FakeServiceBuilder
// FakeServiceBuilder constructs a testServiceBuilder for use in unit tests. It is the
// caller's responsibility to always call Cleanup() on the return value to ensure that
// file system changes are removed when this test ends.
func FakeServiceBuilder() (s *testServiceBuilder) {
root, err := ioutil.TempDir("", "runit_test")
if err != nil {
panic(err)
}
defer func() {
// If the method exits abnormally, try to clean up the file system.
if s == nil {
os.RemoveAll(root)
}
}()
config := filepath.Join(root, "config")
mustMkdirAll(config)
staging := filepath.Join(root, "staging")
mustMkdirAll(staging)
install := filepath.Join(root, "service")
mustMkdirAll(install)
bin := util.From(runtime.Caller(0)).ExpandPath("fake_servicebuilder")
return &testServiceBuilder{
root: root,
ServiceBuilder: ServiceBuilder{
ConfigRoot: config,
StagingRoot: staging,
RunitRoot: install,
Bin: bin,
testingNoChown: true,
},
}
}
示例6: postHelloManifest
func postHelloManifest(dir string) error {
hello := fmt.Sprintf("file://%s", util.From(runtime.Caller(0)).ExpandPath("../hoisted-hello_def456.tar.gz"))
manifest := &pods.Manifest{}
manifest.Id = "hello"
stanza := pods.LaunchableStanza{
LaunchableId: "hello",
LaunchableType: "hoist",
Location: hello,
}
manifest.LaunchableStanzas = map[string]pods.LaunchableStanza{
"hello": stanza,
}
manifestPath := path.Join(dir, "hello.yaml")
f, err := os.OpenFile(manifestPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()
err = manifest.Write(f)
if err != nil {
return err
}
f.Close()
manifestPath, err = signManifest(manifestPath, dir)
if err != nil {
return err
}
return exec.Command("p2-schedule", manifestPath).Run()
}
示例7: postHelloManifest
func postHelloManifest(dir string) error {
hello := fmt.Sprintf("file://%s", util.From(runtime.Caller(0)).ExpandPath("../hoisted-hello_def456.tar.gz"))
builder := pods.NewManifestBuilder()
builder.SetID("hello")
builder.SetStatusPort(43770)
stanzas := map[string]pods.LaunchableStanza{
"hello": {
LaunchableId: "hello",
LaunchableType: "hoist",
Location: hello,
},
}
builder.SetLaunchables(stanzas)
manifest := builder.GetManifest()
manifestPath := path.Join(dir, "hello.yaml")
f, err := os.OpenFile(manifestPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()
err = manifest.Write(f)
if err != nil {
return err
}
f.Close()
manifestPath, err = signManifest(manifestPath, dir)
if err != nil {
return err
}
return exec.Command("p2-schedule", manifestPath).Run()
}
示例8: getConsulManifest
func getConsulManifest(dir string) (string, error) {
consulTar := fmt.Sprintf(
"file://%s",
util.From(runtime.Caller(0)).ExpandPath("../hoisted-consul_052.tar.gz"),
)
builder := pods.NewManifestBuilder()
builder.SetID("consul")
stanzas := map[string]pods.LaunchableStanza{
"consul": {
LaunchableId: "consul",
LaunchableType: "hoist",
Location: consulTar,
},
}
builder.SetLaunchables(stanzas)
manifest := builder.GetManifest()
consulPath := path.Join(dir, "consul.yaml")
f, err := os.OpenFile(consulPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return "", err
}
defer f.Close()
err = manifest.Write(f)
if err != nil {
return "", err
}
return consulPath, f.Close()
}
示例9: writeHelloManifest
// Writes a pod manifest for the hello pod at with the specified name in the
// specified dir, configured to run on the specified port. Returns the path to
// the signed manifest
func writeHelloManifest(dir string, manifestName string, port int) (string, error) {
hello := fmt.Sprintf("file://%s", util.From(runtime.Caller(0)).ExpandPath("../hoisted-hello_def456.tar.gz"))
builder := manifest.NewBuilder()
builder.SetID("hello")
builder.SetStatusPort(port)
builder.SetStatusHTTP(true)
stanzas := map[launch.LaunchableID]launch.LaunchableStanza{
"hello": {
LaunchableType: "hoist",
Location: hello,
},
}
builder.SetLaunchables(stanzas)
builder.SetConfig(map[interface{}]interface{}{
"port": port,
})
manifest := builder.GetManifest()
manifestPath := filepath.Join(dir, manifestName)
f, err := os.OpenFile(manifestPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return "", err
}
defer f.Close()
err = manifest.Write(f)
if err != nil {
return "", err
}
return signManifest(manifestPath, dir)
}
示例10: testManifest
func testManifest(t *testing.T) pods.Manifest {
manifestPath := util.From(runtime.Caller(0)).ExpandPath("test_manifest.yaml")
manifest, err := pods.ManifestFromPath(manifestPath)
if err != nil {
t.Fatal("No test manifest found, failing\n")
}
return manifest
}
示例11: testHookListener
func testHookListener(t *testing.T) (HookListener, string, <-chan struct{}) {
hookPrefix := kp.HOOK_TREE
destDir, _ := ioutil.TempDir("", "pods")
defer os.RemoveAll(destDir)
execDir, err := ioutil.TempDir("", "exec")
defer os.RemoveAll(execDir)
Assert(t).IsNil(err, "should not have erred creating a tempdir")
current, err := user.Current()
Assert(t).IsNil(err, "test setup: could not get the current user")
builder := manifest.NewBuilder()
builder.SetID("users")
builder.SetRunAsUser(current.Username)
builder.SetLaunchables(map[launch.LaunchableID]launch.LaunchableStanza{
"create": {
Location: util.From(runtime.Caller(0)).ExpandPath("hoisted-hello_def456.tar.gz"),
LaunchableType: "hoist",
LaunchableId: "create",
},
})
podManifest := builder.GetManifest()
manifestBytes, err := podManifest.Marshal()
Assert(t).IsNil(err, "manifest bytes error should have been nil")
fakeSigner, err := openpgp.NewEntity("p2", "p2-test", "[email protected]", nil)
Assert(t).IsNil(err, "NewEntity error should have been nil")
var buf bytes.Buffer
sigWriter, err := clearsign.Encode(&buf, fakeSigner.PrivateKey, nil)
Assert(t).IsNil(err, "clearsign encode error should have been nil")
sigWriter.Write(manifestBytes)
sigWriter.Close()
podManifest, err = manifest.FromBytes(buf.Bytes())
Assert(t).IsNil(err, "should have generated manifest from signed bytes")
fakeIntent := fakeStoreWithManifests(kp.ManifestResult{
Manifest: podManifest,
})
hookFactory := pods.NewHookFactory(destDir, "testNode")
listener := HookListener{
Intent: fakeIntent,
HookPrefix: hookPrefix,
ExecDir: execDir,
HookFactory: hookFactory,
Logger: logging.DefaultLogger,
authPolicy: auth.FixedKeyringPolicy{openpgp.EntityList{fakeSigner}, nil},
artifactVerifier: auth.NopVerifier(),
artifactRegistry: artifact.NewRegistry(nil, uri.DefaultFetcher, osversion.DefaultDetector),
}
return listener, destDir, fakeIntent.quit
}
示例12: generatePreparerPod
func generatePreparerPod(workdir string) (string, error) {
// build the artifact from HEAD
err := exec.Command("go", "build", "github.com/square/p2/bin/p2-preparer").Run()
if err != nil {
return "", util.Errorf("Couldn't build preparer: %s", err)
}
wd, _ := os.Getwd()
hostname, err := os.Hostname()
if err != nil {
return "", util.Errorf("Couldn't get hostname: %s", err)
}
// the test number forces the pod manifest to change every test run.
testNumber := fmt.Sprintf("test=%d", rand.Intn(2000000000))
cmd := exec.Command("p2-bin2pod", "--work-dir", workdir, "--id", "p2-preparer", "--config", fmt.Sprintf("node_name=%s", hostname), "--config", testNumber, wd+"/p2-preparer")
manifestPath, err := executeBin2Pod(cmd)
if err != nil {
return "", err
}
manifest, err := pods.ManifestFromPath(manifestPath)
if err != nil {
return "", err
}
builder := manifest.GetBuilder()
builder.SetID("p2-preparer")
builder.SetConfig(map[interface{}]interface{}{
"preparer": map[interface{}]interface{}{
"auth": map[string]string{
"type": "keyring",
"keyring": util.From(runtime.Caller(0)).ExpandPath("pubring.gpg"),
},
"ca_file": filepath.Join(certpath, "cert.pem"),
"cert_file": filepath.Join(certpath, "cert.pem"),
"key_file": filepath.Join(certpath, "key.pem"),
"status_port": preparerStatusPort,
},
})
builder.SetRunAsUser("root")
builder.SetStatusPort(preparerStatusPort)
builder.SetStatusHTTP(true)
manifest = builder.GetManifest()
manifestBytes, err := manifest.Marshal()
if err != nil {
return "", err
}
err = ioutil.WriteFile(manifestPath, manifestBytes, 0644)
if err != nil {
return "", err
}
return manifestPath, err
}
示例13: TestInstall
func TestInstall(t *testing.T) {
fetcher := uri.NewLoggedFetcher(nil)
testContext := util.From(runtime.Caller(0))
currentUser, err := user.Current()
Assert(t).IsNil(err, "test setup: couldn't get current user")
testLocation := testContext.ExpandPath("hoisted-hello_3c021aff048ca8117593f9c71e03b87cf72fd440.tar.gz")
launchables := map[launch.LaunchableID]launch.LaunchableStanza{
"hello": {
LaunchableId: "hello",
Location: testLocation,
LaunchableType: "hoist",
},
}
builder := manifest.NewBuilder()
builder.SetID("hello")
builder.SetLaunchables(launchables)
builder.SetRunAsUser(currentUser.Username)
manifest := builder.GetManifest()
testPodDir, err := ioutil.TempDir("", "testPodDir")
Assert(t).IsNil(err, "Got an unexpected error creating a temp directory")
defer os.RemoveAll(testPodDir)
pod := Pod{
Id: "testPod",
home: testPodDir,
logger: Log.SubLogger(logrus.Fields{"pod": "testPod"}),
Fetcher: fetcher,
}
err = pod.Install(manifest, auth.NopVerifier(), artifact.NewRegistry(nil, uri.DefaultFetcher, osversion.DefaultDetector))
Assert(t).IsNil(err, "there should not have been an error when installing")
Assert(t).AreEqual(
fetcher.SrcUri.String(),
testLocation,
"The correct url wasn't set for the curl library",
)
hoistedHelloUnpacked := filepath.Join(testPodDir, "hello", "installs", "hello_3c021aff048ca8117593f9c71e03b87cf72fd440")
if info, err := os.Stat(hoistedHelloUnpacked); err != nil || !info.IsDir() {
t.Fatalf("Expected %s to be the unpacked artifact location", hoistedHelloUnpacked)
}
helloLaunch := filepath.Join(hoistedHelloUnpacked, "bin", "launch")
if info, err := os.Stat(helloLaunch); err != nil || info.IsDir() {
t.Fatalf("Expected %s to be a the launch script for hello", helloLaunch)
}
}
示例14: TestURIWithNoProtocolTreatedLikeLocalPath
func TestURIWithNoProtocolTreatedLikeLocalPath(t *testing.T) {
tempdir, err := ioutil.TempDir("", "cp-dest")
Assert(t).IsNil(err, "Couldn't create temp dir")
defer os.RemoveAll(tempdir)
thisFile := util.From(runtime.Caller(0)).Filename
copied := filepath.Join(tempdir, "copied")
err = URICopy(thisFile, copied)
Assert(t).IsNil(err, "The file should have been copied")
copiedContents, err := ioutil.ReadFile(copied)
thisContents, err := ioutil.ReadFile(thisFile)
Assert(t).IsNil(err, "The original file could not be read")
Assert(t).AreEqual(string(thisContents), string(copiedContents), "The contents of the files do not match")
}
示例15: createHelloReplicationController
func createHelloReplicationController(dir string) (fields.ID, error) {
hello := fmt.Sprintf("file://%s", util.From(runtime.Caller(0)).ExpandPath("../hoisted-hello_def456.tar.gz"))
builder := manifest.NewBuilder()
builder.SetID("hello")
builder.SetStatusPort(43770)
stanzas := map[launch.LaunchableID]launch.LaunchableStanza{
"hello": {
LaunchableId: "hello",
LaunchableType: "hoist",
Location: hello,
},
}
builder.SetLaunchables(stanzas)
manifest := builder.GetManifest()
manifestPath := path.Join(dir, "hello.yaml")
f, err := os.OpenFile(manifestPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return fields.ID(""), err
}
defer f.Close()
err = manifest.Write(f)
if err != nil {
return fields.ID(""), err
}
f.Close()
manifestPath, err = signManifest(manifestPath, dir)
if err != nil {
return fields.ID(""), err
}
cmd := exec.Command("p2-rctl", "--log-json", "create", "--manifest", manifestPath, "--node-selector", "test=yes")
out := bytes.Buffer{}
cmd.Stdout = &out
cmd.Stderr = &out
err = cmd.Run()
if err != nil {
return fields.ID(""), fmt.Errorf("Couldn't create replication controller for hello: %s %s", out.String(), err)
}
var rctlOut struct {
ID string `json:"id"`
}
err = json.Unmarshal(out.Bytes(), &rctlOut)
if err != nil {
return fields.ID(""), fmt.Errorf("Couldn't read RC ID out of p2-rctl invocation result: %v", err)
}
return fields.ID(rctlOut.ID), exec.Command("p2-rctl", "set-replicas", rctlOut.ID, "1").Run()
}