Exemple #1
0
func isKeyExist(keyDir string) bool {
	if !utils.IsFileExist(filepath.Join(keyDir, defaultPrivateKey)) {
		return false
	}

	if !utils.IsFileExist(filepath.Join(keyDir, defaultPublicKey)) {
		return false
	}

	return true
}
Exemple #2
0
func SaveLayerLocal(srcPath, srcFile, dstPath, dstFile string, reqbody []byte) (int, error) {
	if !utils.IsDirExist(dstPath) {
		os.MkdirAll(dstPath, os.ModePerm)
	}

	if utils.IsFileExist(dstFile) {
		os.Remove(dstFile)
	}

	var data []byte
	if _, err := os.Stat(srcFile); err == nil {
		data, _ = ioutil.ReadFile(srcFile)
		if err := ioutil.WriteFile(dstFile, data, 0777); err != nil {
			return 0, err
		}
		os.RemoveAll(srcPath)
	} else {
		data = reqbody
		if err := ioutil.WriteFile(dstFile, data, 0777); err != nil {
			return 0, err
		}
	}

	return len(data), nil
}
func TestUtilsContainer(t *testing.T) {
	//TODO: dockyard dev team should provide small testing containers.
	imageName := "google/nodejs"
	containerName := ""
	cached, err := utils.IsImageCached(imageName)
	if err == utils.ErrorsNoDockerClient {
		fmt.Println("Please start a docker daemon to continue the container operation test")
		return
	}

	assert.Nil(t, err, "Fail to load Image")

	if !cached {
		err := utils.PullImage(imageName)
		assert.Nil(t, err, "Fail to pull image")
	}

	tmpFile, err := ioutil.TempFile("/tmp", "dockyard-test-container-oper")
	assert.Nil(t, err, "System err, fail to create temp file")
	defer os.Remove(tmpFile.Name())

	var config container.Config
	config.Image = imageName
	config.Cmd = []string{"touch", tmpFile.Name()}
	var hostConfig container.HostConfig
	hostConfig.Binds = append(hostConfig.Binds, "/tmp:/tmp")

	utils.StartContainer(config, hostConfig, containerName)
	//TODO: stop, remove the container process

	assert.Equal(t, true, utils.IsFileExist(tmpFile.Name()), "Fail to touch file by using StartContainer")

}
Exemple #4
0
// Get gets the data of an application
func (r LocalRepo) Get(name string) ([]byte, error) {
	var meta utils.Meta
	metaFile := r.GetMetaFile()
	if !utils.IsFileExist(metaFile) {
		return nil, ErrorEmptyLocalRepo
	}
	data, err := ioutil.ReadFile(metaFile)
	if err != nil {
		return nil, err
	}

	err = json.Unmarshal(data, &meta)
	if err != nil {
		//This may happend in migration, meta struct changes.
		os.Remove(metaFile)
		return nil, nil
	}

	for i := range meta.Items {
		if meta.Items[i].Name != name {
			continue
		}

		dataFile := filepath.Join(r.GetTopDir(), defaultTargetDir, meta.Items[i].Hash)
		return ioutil.ReadFile(dataFile)
	}

	return nil, ErrorAppNotExist
}
Exemple #5
0
// GetMeta gets the meta data of a repository
func (r LocalRepo) GetMeta() ([]byte, error) {
	metaFile := r.GetMetaFile()
	if !utils.IsFileExist(metaFile) {
		return nil, ErrorEmptyLocalRepo
	}

	return ioutil.ReadFile(metaFile)
}
Exemple #6
0
func runWeb(c *cli.Context) error {
	startServerService()

	m := macaron.New()

	//Set Macaron Web Middleware And Routers
	web.SetDockyardMacaron(m)

	switch setting.ListenMode {
	case "http":
		listenaddr := fmt.Sprintf("%s:%d", c.String("address"), c.Int("port"))
		if err := http.ListenAndServe(listenaddr, m); err != nil {
			fmt.Printf("Start Dockyard http service error: %v\n", err.Error())
			return err
		}
		break
	case "https":
		listenaddr := fmt.Sprintf("%s:443", c.String("address"))
		server := &http.Server{Addr: listenaddr, TLSConfig: &tls.Config{MinVersion: tls.VersionTLS10}, Handler: m}
		if err := server.ListenAndServeTLS(setting.HTTPSCertFile, setting.HTTPSKeyFile); err != nil {
			fmt.Printf("Start Dockyard https service error: %v\n", err.Error())
			return err
		}
		break
	case "unix":
		listenaddr := fmt.Sprintf("%s", c.String("address"))
		if utils.IsFileExist(listenaddr) {
			os.Remove(listenaddr)
		}

		if listener, err := net.Listen("unix", listenaddr); err != nil {
			fmt.Printf("Start Dockyard unix socket error: %v\n", err.Error())
			return err
		} else {
			server := &http.Server{Handler: m}
			if err := server.Serve(listener); err != nil {
				fmt.Printf("Start Dockyard unix socket error: %v\n", err.Error())
				return err
			}
		}
		break
	default:
		break
	}

	return nil
}
Exemple #7
0
// Load reads the config data
func (ucc *UpdateClientConfig) Load() error {
	homeDir := os.Getenv("HOME")
	if homeDir == "" {
		return errors.New("Cannot get home directory")
	}

	if !utils.IsFileExist(filepath.Join(homeDir, topDir, configName)) {
		ucc.Init()
	}

	content, err := ioutil.ReadFile(filepath.Join(homeDir, topDir, configName))
	if err != nil {

		return err
	}

	if err := json.Unmarshal(content, &ucc); err != nil {
		return err
	}

	return nil
}
Exemple #8
0
// Delete removes an application from a repository
func (r LocalRepo) Delete(name string) error {
	var meta utils.Meta
	metaFile := r.GetMetaFile()
	if !utils.IsFileExist(metaFile) {
		return ErrorEmptyLocalRepo
	}
	data, err := ioutil.ReadFile(metaFile)
	if err != nil {
		return err
	}

	err = json.Unmarshal(data, &meta)
	if err != nil {
		//This may happend in migration, meta struct changes.
		os.Remove(metaFile)
		return nil
	}

	for i := range meta.Items {
		if meta.Items[i].Name != name {
			continue
		}

		dataFile := filepath.Join(r.GetTopDir(), defaultTargetDir, meta.Items[i].Hash)
		if err := os.Remove(dataFile); err != nil {
			return err
		}

		meta.Items = append(meta.Items[:i], meta.Items[i+1:]...)
		if err := r.saveMeta(meta); err != nil {
			return err
		}
		return nil
	}

	return ErrorAppNotExist
}
Exemple #9
0
func (ucc *UpdateClientConfig) exist() bool {
	configFile := filepath.Join(os.Getenv("HOME"), topDir, configName)
	return utils.IsFileExist(configFile)
}
// TestSLRepoBasic
func TestSLRepoBasic(t *testing.T) {
	topDir, err := ioutil.TempDir("", "dus-repo-test-")
	defer os.RemoveAll(topDir)
	assert.Nil(t, err, "Fail to create temp dir")

	proto := "app/v1"
	invalidNRs := []string{"", "a/b/c"}
	for _, invalidNR := range invalidNRs {
		_, err := storage.NewLocalRepo(topDir, proto, invalidNR)
		assert.NotNil(t, err, "Fail to return error while setup an invalid url")
	}

	// new
	validNR := "containerops/official"
	validNamespace := "containerops"
	r, err := storage.NewLocalRepo(topDir, proto, validNR)
	assert.Nil(t, err, "Fail to setup a valid url")
	assert.Equal(t, r.GetTopDir(), filepath.Join(topDir, proto, validNR), "Fail to get the correct top dir")
	assert.Equal(t, r.GetMetaFile(), filepath.Join(topDir, proto, validNR, "meta.json"), "Fail to get the default meta file")

	err = r.SetKM(topDir)
	assert.Nil(t, err, "Fail to set key manager")

	// add
	testData := map[string]string{
		"appA": "This is the content of appA.",
		"appB": "This is the content of appB.",
	}

	for name, value := range testData {
		_, err := r.Put(name, []byte(value), utils.EncryptNone)
		assert.Nil(t, err, "Fail to add a file")
	}

	// list
	names, err := r.List()
	assert.Nil(t, err, "Fail to list repo files")
	assert.Equal(t, len(names), len(testData), "Fail to add or list the same number")
	for _, name := range names {
		_, ok := testData[name]
		assert.Equal(t, ok, true, "Fail to list the correct data")
	}

	// remove
	removeFile := "appA"
	err = r.Delete(removeFile)
	assert.Nil(t, err, "Fail to remove valid file")
	err = r.Delete(removeFile)
	assert.NotNil(t, err, "Fail to remove invalid file")

	// get removed file
	_, err = r.Get(removeFile)
	assert.NotNil(t, err, "Fail to get removed file")

	// update (add with a exist name)
	updateFile := "appB"
	updateContent := "This is the content of updated appB."
	_, err = r.Put(updateFile, []byte(updateContent), utils.EncryptNone)
	assert.Nil(t, err, "Fail to add an exist file")

	res, err := r.Get(updateFile)
	assert.Nil(t, err, "Fail to get file")
	assert.Equal(t, string(res), updateContent)

	// update with encrypt gpg method
	_, err = r.Put(updateFile, []byte(updateContent), utils.EncryptRSA)
	assert.Nil(t, err, "Fail to add an exist file")

	encryptdRes, err := r.Get(updateFile)
	assert.Nil(t, err, "Fail to get file")
	assert.NotEqual(t, string(encryptdRes), updateContent)

	// decrypt by keymanager
	k, err := km.NewKeyManager(topDir)
	assert.Nil(t, err, "Fail to get key manager")
	decryptdRes, err := k.Decrypt(proto, validNamespace, encryptdRes)
	assert.Nil(t, err, "Fail to decrypt")
	assert.Equal(t, string(decryptdRes), updateContent)

	// get meta
	_, err = r.GetMeta()
	assert.Nil(t, err, "Fail to get meta data")

	// get metasign
	metaSignFile := r.GetMetaSignFile()
	ok := utils.IsFileExist(metaSignFile)
	assert.Equal(t, ok, true, "Fail to generate meta sign file")

	// get public key
	pubKeyFile := r.GetPublicKeyFile()
	ok = utils.IsFileExist(pubKeyFile)
	assert.Equal(t, ok, true, "Fail to generate public key file")
}
Exemple #11
0
// Put adds an application to a repository
func (r LocalRepo) Put(name string, content []byte, method utils.EncryptMethod) (string, error) {
	topDir := r.GetTopDir()
	if !utils.IsDirExist(topDir) {
		if err := os.MkdirAll(topDir, 0777); err != nil {
			return "", err
		}
	}

	var meta utils.Meta
	metaFile := r.GetMetaFile()
	if utils.IsFileExist(metaFile) {
		data, err := ioutil.ReadFile(metaFile)
		if err != nil {
			return "", err
		}

		err = json.Unmarshal(data, &meta)
		if err != nil {
			//This may happend in migration, meta struct changes.
			os.Remove(metaFile)
		}

	}

	encryptContent, err := r.encrypt(method, content)
	if err != nil {
		return "", err
	}

	item := utils.GenerateMetaItem(name, encryptContent)
	item.SetEncryption(method)

	// Using the 'hash' value to rename the original file
	dataFileName := item.GetHash()
	dataFile := filepath.Join(topDir, defaultTargetDir, dataFileName)
	if !utils.IsDirExist(filepath.Dir(dataFile)) {
		if err := os.MkdirAll(filepath.Dir(dataFile), 0777); err != nil {
			return "", err
		}
	}

	// write data
	if err := ioutil.WriteFile(dataFile, encryptContent, 0644); err != nil {
		return "", err
	}

	// get meta content
	exist := false
	for i := range meta.Items {
		if meta.Items[i].Name == name {
			meta.Items[i] = item
			exist = true
		}
	}
	if !exist {
		meta.Items = append(meta.Items, item)
	}

	// write meta data
	err = r.saveMeta(meta)
	if err != nil {
		os.Remove(dataFile)
		return "", err
	}

	return dataFile, nil
}