Skip to content

Commit

Permalink
Move the setting from dockyard module to common module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Meaglith Ma committed Jun 22, 2017
1 parent e08afe7 commit a7381dc
Show file tree
Hide file tree
Showing 14 changed files with 264 additions and 232 deletions.
199 changes: 199 additions & 0 deletions common/configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
Copyright 2016 - 2017 Huawei Technologies Co., Ltd. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package common

import (
"fmt"

"encoding/json"
homeDir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)

// SetConfig is setting config file path/name/type.
func SetConfig(cfgFile string) error {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homeDir.Dir()
if err != nil {
return fmt.Errorf("Read $HOME envrionment error: %s", err.Error())
}

// Search config in home directory with name "containerops" (without extension).
viper.SetConfigType("toml")
viper.SetConfigName("containerops")
viper.AddConfigPath("/etc/containerops/config")
viper.AddConfigPath(fmt.Sprintf("%s/.containerops/config", home))
viper.AddConfigPath(".")
}

viper.SetEnvPrefix("coops")
viper.AutomaticEnv() // read in environment variables that match

if err := viper.ReadInConfig(); err != nil {
return fmt.Errorf("Fatal error config file: %s", err.Error())
}

if err := setDatabaseConfig(viper.GetStringMap("database")); err != nil {
return err
}

if err := setWebConfig(viper.GetStringMap("web")); err != nil {
return err
}

if err := setStorageConfig(viper.GetStringMap("storage")); err != nil {
return err
}

if err := setWarshipConfig(viper.GetStringMap("warship")); err != nil {
return err
}

return nil
}

/*
Configurations for all modules
# 1. Configurations of database.
[database]
driver = "mysql"
host = "127.0.0.1"
port = 3306
user = "root"
password = "containerops_database"
db = "containerops_password"
# 2. Configurations for HTTPS or Unix Socket
# 2.1 If multi modules deploy in one node, there should have a proxy like Caddy or Nginx.
# Each module use with Unix Socket type, configurations look like this:
#
# [web]
# mode = "unix"
# address = "/var/run/${module}.socket"
#
# 2.2 If module deploys in one node alone, it only supports HTTPS model and must have the SSL
# certification files.
[web]
domain = "opshub.sh"
mode = "https"
address = "127.0.0.1"
port = 443
cert = "PATH_TO_CERT_FILE"
key = "PATH_TO_KEY_FILE"
# 3. Configurations for storage path of Dockyard module.
# 3.1 TODO Using the Object Storage Service in the Dockyard module.
[storage]
dockerv2 = "/tmp/dockerv2" # path for image files of Docker Distribution V2 Protocol
binaryv1 = "/tmp/binaryv1" # path for binary files of Dockyard Binary V1 Protocol
# 4. Configurations for Warship of Dockyard client.
[warship]
domain = "hub.opshub.sh"
*/

type DatabaseConfig struct {
Driver string `json:"driver"`
Host string `json:"host"`
Port int `json:"port"`
User string `json:"user"`
Password string `json:"password"`
Name string `json:"db"`
}

type WebConfig struct {
Domain string `json:"domain" description:"Listen domain, the official domain is *.osphub.sh"`
Mode string `json:"mode" description:"Listen mode, 'https' or 'unix'"`
Address string `json:"address" description:"The host address when mode is 'https', or socket file path when mode is 'unix'"`
Port int `json:"port"`
Key string `json:"key"`
Cert string `json:"cert"`
}

type StorageConfig struct {
DockerV2 string `json:"dockerv2" description:"Docker V2 images path in the host."`
BinaryV1 string `json:"binaryv1" description:"Binary V1 files path in the host"`
}

type WarshipConfig struct {
Domain string
}

var Database DatabaseConfig
var Web WebConfig
var Storage StorageConfig
var Warship WarshipConfig

func setDatabaseConfig(config map[string]interface{}) error {
bs, err := json.Marshal(&config)
if err != nil {
return err
}

return json.Unmarshal(bs, &Database)
}

func setWebConfig(config map[string]interface{}) error {
bs, err := json.Marshal(&config)
if err != nil {
return err
}

err = json.Unmarshal(bs, &Web)
if err != nil {
return err
}

return nil
}

func setStorageConfig(config map[string]interface{}) error {
bs, err := json.Marshal(&config)
if err != nil {
return err
}

err = json.Unmarshal(bs, &Storage)
if err != nil {
return err
}

return nil
}

func setWarshipConfig(config map[string]interface{}) error {
bs, err := json.Marshal(&config)
if err != nil {
return err
}

err = json.Unmarshal(bs, &Warship)
if err != nil {
return err
}

return nil
}
25 changes: 10 additions & 15 deletions dockyard/client/cmd/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,26 @@ package cmd

import (
"fmt"
//"os"

"github.com/spf13/cobra"

"github.com/Huawei/containerops/common"
)

var namespace, repository, repoType string
var repoType string

// repository sub command
var repositoryCmd = &cobra.Command{
Use: "repository",
Short: "repository sub command which create/delete and other manage repository.",
Long: `When using Dockyard as binary repository, should create a repository with
binary type.`,
binary type before uploading file.`,
}

// create repository command
var createRepositoryCmd = &cobra.Command{
Use: "create",
Short: "Create a repository in Dockyard",
Short: "Namespace and repository as the args like `containerops/cncf-demo`.",
Long: `There are two repository types support just now, it's docker and binary`,
Run: createRepository,
}
Expand All @@ -47,20 +48,14 @@ func init() {

//Add create repository sub command.
repositoryCmd.AddCommand(createRepositoryCmd)
repositoryCmd.PersistentFlags().StringVarP(&namespace, "user", "u", "", "Username or Organization for repository")
repositoryCmd.PersistentFlags().StringVarP(&repository, "repo", "r", "", "Repository name")
repositoryCmd.PersistentFlags().StringVarP(&repoType, "type", "t", "", "Repository type")

createRepositoryCmd.Flags().StringVarP(&repoType, "type", "t", "", "Repository type")
}

// createRepository is
func createRepository(cmd *cobra.Command, args []string) {
//if domain == "" || namespace == "" || repository == "" || repoType == "" {
// fmt.Println("Parameter may not be empty. ")
// os.Exit(1)
//}
//
//uri := fmt.Sprintf("https://%s/v1/%s/%s/%s", domain, namespace, repository, repoType)
//fmt.Println(uri)
if domain == "" {
domain = common.Warship.Domain
}

fmt.Println(domain)
}
34 changes: 8 additions & 26 deletions dockyard/client/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"fmt"
"os"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/Huawei/containerops/common"
)

var cfgFile, domain string
Expand All @@ -41,10 +42,11 @@ And we also working on push/pull Docker image, rkt ACI and OCI Image.`,
func init() {
cobra.OnInitialize(initConfig)

RootCmd.Flags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.containerops/config/containerops)")
RootCmd.Flags().StringVarP(&domain, "domain", "d", "", "dockyard service domain")
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "Configuration file path")
RootCmd.PersistentFlags().StringVar(&domain, "domain", "", "Dockyard service domain")

viper.BindPFlag("domain", RootCmd.Flags().Lookup("domain"))
viper.BindPFlag("config", RootCmd.Flags().Lookup("config"))
}

// Execute adds all child commands to the root command sets flags appropriately.
Expand All @@ -58,28 +60,8 @@ func Execute() {

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Search config in home directory with name "containerops" (without extension).
viper.AddConfigPath("/etc/containerops/config")
viper.AddConfigPath(fmt.Sprintf("%s/.containerops/config", home))
viper.AddConfigPath(".")
viper.SetConfigName("containerops")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
if err := common.SetConfig(cfgFile); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
File renamed without changes.
Loading

0 comments on commit a7381dc

Please sign in to comment.