template_handler.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package api
  2. import (
  3. "archive/tar"
  4. "bytes"
  5. "compress/gzip"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "net/http"
  12. "strings"
  13. "gopkg.in/yaml.v2"
  14. )
  15. var baseURL string = "https://porter-dev.github.io/chart-repo/"
  16. // IndexYAML represents a chart repo's index.yaml
  17. type IndexYAML struct {
  18. APIVersion string `yaml:"apiVersion"`
  19. Generated string `yaml:"generated"`
  20. Entries map[interface{}]ChartYAML `yaml:"entries"`
  21. }
  22. // ChartYAML represents the data for chart in index.yaml
  23. type ChartYAML []struct {
  24. APIVersion string `yaml:"apiVersion"`
  25. AppVersion string `yaml:"appVersion"`
  26. Created string `yaml:"created"`
  27. Description string `yaml:"description"`
  28. Digest string `yaml:"digest"`
  29. Icon string `yaml:"icon"`
  30. Name string `yaml:"name"`
  31. Type string `yaml:"type"`
  32. Urls []string `yaml:"urls"`
  33. Version string `yaml:"version"`
  34. }
  35. // PorterChart represents a bundled Porter template
  36. type PorterChart struct {
  37. Name string
  38. Description string
  39. Icon string
  40. Form FormYAML
  41. }
  42. // FormYAML represents a chart's values.yaml form abstraction
  43. type FormYAML struct {
  44. Name string `yaml:"name"`
  45. Icon string `yaml:"icon"`
  46. Description string `yaml:"description"`
  47. Tags []string `yaml:"tags"`
  48. Sections []struct {
  49. Name string `yaml:"name"`
  50. Contents []struct {
  51. Type string `yaml:"type"`
  52. Label string `yaml:"label"`
  53. Name string `yaml:"name,omitempty"`
  54. Variable string `yaml:"variable,omitempty"`
  55. Settings struct {
  56. Default int `yaml:"default"`
  57. } `yaml:"settings,omitempty"`
  58. } `yaml:"contents"`
  59. } `yaml:"sections"`
  60. }
  61. // HandleListTemplates retrieves a list of Porter templates
  62. // TODO: test and reduce fragility (handle untar/parse error for individual charts)
  63. func (app *App) HandleListTemplates(w http.ResponseWriter, r *http.Request) {
  64. resp, err := http.Get(baseURL + "index.yaml")
  65. if err != nil {
  66. fmt.Println(err)
  67. return
  68. }
  69. defer resp.Body.Close()
  70. body, _ := ioutil.ReadAll(resp.Body)
  71. form := IndexYAML{}
  72. if err := yaml.Unmarshal([]byte(body), &form); err != nil {
  73. fmt.Println(err)
  74. return
  75. }
  76. // Loop over charts in index.yaml
  77. porterCharts := []PorterChart{}
  78. for k := range form.Entries {
  79. indexChart := form.Entries[k][0]
  80. tarURL := indexChart.Urls[0]
  81. if !strings.Contains(tarURL, "http://") {
  82. tarURL = baseURL + tarURL
  83. }
  84. formData, err := getFormData(tarURL)
  85. if err != nil {
  86. fmt.Println(err)
  87. return
  88. }
  89. porterChart := PorterChart{}
  90. porterChart.Name = indexChart.Name
  91. porterChart.Description = indexChart.Description
  92. porterChart.Icon = indexChart.Icon
  93. porterChart.Form = *formData
  94. porterCharts = append(porterCharts, porterChart)
  95. }
  96. json.NewEncoder(w).Encode(porterCharts)
  97. }
  98. func getFormData(tarURL string) (*FormYAML, error) {
  99. resp, err := http.Get(tarURL)
  100. if err != nil {
  101. fmt.Println(err)
  102. return nil, err
  103. }
  104. defer resp.Body.Close()
  105. body, _ := ioutil.ReadAll(resp.Body)
  106. buf := bytes.NewBuffer(body)
  107. gzf, err := gzip.NewReader(buf)
  108. if err != nil {
  109. fmt.Println(err)
  110. return nil, err
  111. }
  112. // Process tarball to generate FormYAML
  113. tarReader := tar.NewReader(gzf)
  114. for {
  115. header, err := tarReader.Next()
  116. if err == io.EOF {
  117. break
  118. } else if err != nil {
  119. fmt.Println(err)
  120. return nil, err
  121. }
  122. name := header.Name
  123. switch header.Typeflag {
  124. case tar.TypeDir:
  125. continue
  126. case tar.TypeReg:
  127. // Handle form.yaml located in archive
  128. if strings.Contains(name, "form.yaml") {
  129. bufForm := new(bytes.Buffer)
  130. _, err := io.Copy(bufForm, tarReader)
  131. if err != nil {
  132. fmt.Println(err)
  133. return nil, err
  134. }
  135. // Unmarshal yaml byte buffer
  136. form := FormYAML{}
  137. if err := yaml.Unmarshal(bufForm.Bytes(), &form); err != nil {
  138. fmt.Println(err)
  139. return nil, err
  140. }
  141. return &form, nil
  142. }
  143. default:
  144. fmt.Printf("%s : %c %s %s\n",
  145. "Unknown type",
  146. header.Typeflag,
  147. "in file",
  148. name,
  149. )
  150. }
  151. }
  152. return nil, errors.New("no form.yaml found")
  153. }