template_handler.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. Markdown string
  42. }
  43. // FormYAML represents a chart's values.yaml form abstraction
  44. type FormYAML struct {
  45. Name string `yaml:"name"`
  46. Icon string `yaml:"icon"`
  47. Description string `yaml:"description"`
  48. Tags []string `yaml:"tags"`
  49. Sections []struct {
  50. Name string `yaml:"name"`
  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. resp, err := http.Get(baseURL + "index.yaml")
  67. if err != nil {
  68. fmt.Println(err)
  69. return
  70. }
  71. defer resp.Body.Close()
  72. body, _ := ioutil.ReadAll(resp.Body)
  73. form := IndexYAML{}
  74. if err := yaml.Unmarshal([]byte(body), &form); err != nil {
  75. fmt.Println(err)
  76. return
  77. }
  78. // Loop over charts in index.yaml
  79. porterCharts := []PorterChart{}
  80. for k := range form.Entries {
  81. indexChart := form.Entries[k][0]
  82. tarURL := indexChart.Urls[0]
  83. if !strings.Contains(tarURL, "http://") {
  84. tarURL = baseURL + tarURL
  85. }
  86. formData, markdown, err := processTarball(tarURL)
  87. if err != nil {
  88. fmt.Println(err)
  89. return
  90. }
  91. porterChart := PorterChart{}
  92. porterChart.Name = indexChart.Name
  93. porterChart.Description = indexChart.Description
  94. porterChart.Icon = indexChart.Icon
  95. porterChart.Form = *formData
  96. if markdown != "" {
  97. porterChart.Markdown = markdown
  98. }
  99. porterCharts = append(porterCharts, porterChart)
  100. }
  101. json.NewEncoder(w).Encode(porterCharts)
  102. }
  103. func processTarball(tarURL string) (*FormYAML, string, error) {
  104. resp, err := http.Get(tarURL)
  105. if err != nil {
  106. fmt.Println(err)
  107. return nil, "", err
  108. }
  109. defer resp.Body.Close()
  110. body, _ := ioutil.ReadAll(resp.Body)
  111. buf := bytes.NewBuffer(body)
  112. gzf, err := gzip.NewReader(buf)
  113. if err != nil {
  114. fmt.Println(err)
  115. return nil, "", err
  116. }
  117. // Process tarball to generate FormYAML and retrieve markdown
  118. tarReader := tar.NewReader(gzf)
  119. markdown := ""
  120. for {
  121. header, err := tarReader.Next()
  122. if err == io.EOF {
  123. break
  124. } else if err != nil {
  125. fmt.Println(err)
  126. return nil, "", err
  127. }
  128. name := header.Name
  129. switch header.Typeflag {
  130. case tar.TypeDir:
  131. continue
  132. case tar.TypeReg:
  133. // Handle info.md if found
  134. if strings.Contains(name, "README.md") {
  135. bufMd := new(bytes.Buffer)
  136. _, err := io.Copy(bufMd, tarReader)
  137. if err != nil {
  138. fmt.Println(err)
  139. return nil, "", err
  140. }
  141. markdown = string(bufMd.Bytes())
  142. }
  143. // Handle form.yaml located in archive
  144. if strings.Contains(name, "form.yaml") {
  145. bufForm := new(bytes.Buffer)
  146. _, err := io.Copy(bufForm, tarReader)
  147. if err != nil {
  148. fmt.Println(err)
  149. return nil, "", err
  150. }
  151. // Unmarshal yaml byte buffer
  152. form := FormYAML{}
  153. if err := yaml.Unmarshal(bufForm.Bytes(), &form); err != nil {
  154. fmt.Println(err)
  155. return nil, "", err
  156. }
  157. return &form, markdown, nil
  158. }
  159. default:
  160. fmt.Printf("%s : %c %s %s\n",
  161. "Unknown type",
  162. header.Typeflag,
  163. "in file",
  164. name,
  165. )
  166. }
  167. }
  168. return nil, "", errors.New("no form.yaml found")
  169. }