template_handler.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. // IndexYAML represents a chart repo's index.yaml
  16. type IndexYAML struct {
  17. APIVersion string `yaml:"apiVersion"`
  18. Generated string `yaml:"generated"`
  19. Entries map[interface{}]ChartYAML `yaml:"entries"`
  20. }
  21. // ChartYAML represents the data for chart in index.yaml
  22. type ChartYAML []struct {
  23. APIVersion string `yaml:"apiVersion"`
  24. AppVersion string `yaml:"appVersion"`
  25. Created string `yaml:"created"`
  26. Description string `yaml:"description"`
  27. Digest string `yaml:"digest"`
  28. Icon string `yaml:"icon"`
  29. Name string `yaml:"name"`
  30. Type string `yaml:"type"`
  31. Urls []string `yaml:"urls"`
  32. Version string `yaml:"version"`
  33. }
  34. // PorterChart represents a bundled Porter template
  35. type PorterChart struct {
  36. Name string
  37. Description string
  38. Icon string
  39. Form FormYAML
  40. Markdown string
  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. ShowIf string `yaml:"show_if"`
  51. Contents []struct {
  52. Type string `yaml:"type"`
  53. Label string `yaml:"label"`
  54. Name string `yaml:"name,omitempty"`
  55. Variable string `yaml:"variable,omitempty"`
  56. Settings struct {
  57. Default interface{}
  58. } `yaml:"settings,omitempty"`
  59. } `yaml:"contents"`
  60. } `yaml:"sections"`
  61. }
  62. // HandleListTemplates retrieves a list of Porter templates
  63. // TODO: test and reduce fragility (handle untar/parse error for individual charts)
  64. // TODO: separate markdown retrieval into its own query if necessary
  65. func (app *App) HandleListTemplates(w http.ResponseWriter, r *http.Request) {
  66. baseURL := "https://porter-dev.github.io/chart-repo/"
  67. resp, err := http.Get(baseURL + "index.yaml")
  68. if err != nil {
  69. fmt.Println(err)
  70. return
  71. }
  72. defer resp.Body.Close()
  73. body, _ := ioutil.ReadAll(resp.Body)
  74. form := IndexYAML{}
  75. if err := yaml.Unmarshal([]byte(body), &form); err != nil {
  76. fmt.Println(err)
  77. return
  78. }
  79. // Loop over charts in index.yaml
  80. porterCharts := []PorterChart{}
  81. for k := range form.Entries {
  82. indexChart := form.Entries[k][0]
  83. tarURL := indexChart.Urls[0]
  84. if !strings.Contains(tarURL, "http://") {
  85. tarURL = baseURL + tarURL
  86. }
  87. formData, markdown, err := processTarball(tarURL)
  88. if err != nil {
  89. fmt.Println(err)
  90. return
  91. }
  92. porterChart := PorterChart{}
  93. porterChart.Name = indexChart.Name
  94. porterChart.Description = indexChart.Description
  95. porterChart.Icon = indexChart.Icon
  96. porterChart.Form = *formData
  97. if markdown != "" {
  98. porterChart.Markdown = markdown
  99. }
  100. porterCharts = append(porterCharts, porterChart)
  101. }
  102. json.NewEncoder(w).Encode(porterCharts)
  103. }
  104. func processTarball(tarURL string) (*FormYAML, string, error) {
  105. resp, err := http.Get(tarURL)
  106. if err != nil {
  107. fmt.Println(err)
  108. return nil, "", err
  109. }
  110. defer resp.Body.Close()
  111. body, _ := ioutil.ReadAll(resp.Body)
  112. buf := bytes.NewBuffer(body)
  113. gzf, err := gzip.NewReader(buf)
  114. if err != nil {
  115. fmt.Println(err)
  116. return nil, "", err
  117. }
  118. // Process tarball to generate FormYAML and retrieve markdown
  119. tarReader := tar.NewReader(gzf)
  120. markdown := ""
  121. for {
  122. header, err := tarReader.Next()
  123. if err == io.EOF {
  124. break
  125. } else if err != nil {
  126. fmt.Println(err)
  127. return nil, "", err
  128. }
  129. name := header.Name
  130. switch header.Typeflag {
  131. case tar.TypeDir:
  132. continue
  133. case tar.TypeReg:
  134. // Handle info.md if found
  135. if strings.Contains(name, "README.md") {
  136. bufMd := new(bytes.Buffer)
  137. _, err := io.Copy(bufMd, tarReader)
  138. if err != nil {
  139. fmt.Println(err)
  140. return nil, "", err
  141. }
  142. markdown = string(bufMd.Bytes())
  143. }
  144. // Handle form.yaml located in archive
  145. if strings.Contains(name, "form.yaml") {
  146. bufForm := new(bytes.Buffer)
  147. _, err := io.Copy(bufForm, tarReader)
  148. if err != nil {
  149. fmt.Println(err)
  150. return nil, "", err
  151. }
  152. // Unmarshal yaml byte buffer
  153. form := FormYAML{}
  154. if err := yaml.Unmarshal(bufForm.Bytes(), &form); err != nil {
  155. fmt.Println(err)
  156. return nil, "", err
  157. }
  158. return &form, markdown, nil
  159. }
  160. default:
  161. fmt.Printf("%s : %c %s %s\n",
  162. "Unknown type",
  163. header.Typeflag,
  164. "in file",
  165. name,
  166. )
  167. }
  168. }
  169. return nil, "", errors.New("no form.yaml found")
  170. }