github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+incompatible/pkg/chartutil/create.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package chartutil
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  
    25  	"k8s.io/helm/pkg/proto/hapi/chart"
    26  )
    27  
    28  const (
    29  	// ChartfileName is the default Chart file name.
    30  	ChartfileName = "Chart.yaml"
    31  	// ValuesfileName is the default values file name.
    32  	ValuesfileName = "values.yaml"
    33  	// TemplatesDir is the relative directory name for templates.
    34  	TemplatesDir = "templates"
    35  	// ChartsDir is the relative directory name for charts dependencies.
    36  	ChartsDir = "charts"
    37  	// IgnorefileName is the name of the Helm ignore file.
    38  	IgnorefileName = ".helmignore"
    39  )
    40  
    41  const defaultValues = `# Default values for %s.
    42  # This is a YAML-formatted file.
    43  # Declare name/value pairs to be passed into your templates.
    44  # name: value
    45  `
    46  
    47  const defaultIgnore = `# Patterns to ignore when building packages.
    48  # This supports shell glob matching, relative path matching, and
    49  # negation (prefixed with !). Only one pattern per line.
    50  .DS_Store
    51  # Common VCS dirs
    52  .git/
    53  .gitignore
    54  .bzr/
    55  .bzrignore
    56  .hg/
    57  .hgignore
    58  .svn/
    59  # Common backup files
    60  *.swp
    61  *.bak
    62  *.tmp
    63  *~
    64  # Various IDEs
    65  .project
    66  .idea/
    67  *.tmproj
    68  `
    69  
    70  // Create creates a new chart in a directory.
    71  //
    72  // Inside of dir, this will create a directory based on the name of
    73  // chartfile.Name. It will then write the Chart.yaml into this directory and
    74  // create the (empty) appropriate directories.
    75  //
    76  // The returned string will point to the newly created directory. It will be
    77  // an absolute path, even if the provided base directory was relative.
    78  //
    79  // If dir does not exist, this will return an error.
    80  // If Chart.yaml or any directories cannot be created, this will return an
    81  // error. In such a case, this will attempt to clean up by removing the
    82  // new chart directory.
    83  func Create(chartfile *chart.Metadata, dir string) (string, error) {
    84  	path, err := filepath.Abs(dir)
    85  	if err != nil {
    86  		return path, err
    87  	}
    88  
    89  	if fi, err := os.Stat(path); err != nil {
    90  		return path, err
    91  	} else if !fi.IsDir() {
    92  		return path, fmt.Errorf("no such directory %s", path)
    93  	}
    94  
    95  	n := chartfile.Name
    96  	cdir := filepath.Join(path, n)
    97  	if fi, err := os.Stat(cdir); err == nil && !fi.IsDir() {
    98  		return cdir, fmt.Errorf("file %s already exists and is not a directory", cdir)
    99  	}
   100  	if err := os.MkdirAll(cdir, 0755); err != nil {
   101  		return cdir, err
   102  	}
   103  
   104  	if err := SaveChartfile(filepath.Join(cdir, ChartfileName), chartfile); err != nil {
   105  		return cdir, err
   106  	}
   107  
   108  	val := []byte(fmt.Sprintf(defaultValues, chartfile.Name))
   109  	if err := ioutil.WriteFile(filepath.Join(cdir, ValuesfileName), val, 0644); err != nil {
   110  		return cdir, err
   111  	}
   112  
   113  	val = []byte(defaultIgnore)
   114  	if err := ioutil.WriteFile(filepath.Join(cdir, IgnorefileName), val, 0644); err != nil {
   115  		return cdir, err
   116  	}
   117  
   118  	for _, d := range []string{TemplatesDir, ChartsDir} {
   119  		if err := os.MkdirAll(filepath.Join(cdir, d), 0755); err != nil {
   120  			return cdir, err
   121  		}
   122  	}
   123  	return cdir, nil
   124  }