2017-10-21 17:56:53 +02:00
|
|
|
package module
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-10-26 01:20:05 +02:00
|
|
|
"fmt"
|
2017-10-21 17:56:53 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-10-23 17:34:50 +02:00
|
|
|
|
|
|
|
getter "github.com/hashicorp/go-getter"
|
2017-10-26 01:20:05 +02:00
|
|
|
"github.com/hashicorp/terraform/registry/regsrc"
|
2017-10-27 21:07:24 +02:00
|
|
|
"github.com/hashicorp/terraform/svchost/auth"
|
2017-10-27 17:29:29 +02:00
|
|
|
"github.com/hashicorp/terraform/svchost/disco"
|
|
|
|
"github.com/mitchellh/cli"
|
2017-10-21 17:56:53 +02:00
|
|
|
)
|
|
|
|
|
2017-10-23 17:19:24 +02:00
|
|
|
const manifestName = "modules.json"
|
|
|
|
|
2017-10-21 17:56:53 +02:00
|
|
|
// moduleManifest is the serialization structure used to record the stored
|
|
|
|
// module's metadata.
|
|
|
|
type moduleManifest struct {
|
|
|
|
Modules []moduleRecord
|
|
|
|
}
|
|
|
|
|
|
|
|
// moduleRecords represents the stored module's metadata.
|
|
|
|
// This is compared for equality using '==', so all fields needs to remain
|
|
|
|
// comparable.
|
|
|
|
type moduleRecord struct {
|
2017-10-24 00:07:49 +02:00
|
|
|
// Source is the module source string from the config, minus any
|
|
|
|
// subdirectory.
|
2017-10-21 17:56:53 +02:00
|
|
|
Source string
|
|
|
|
|
2017-10-24 00:07:49 +02:00
|
|
|
// Key is the locally unique identifier for this module.
|
|
|
|
Key string
|
|
|
|
|
|
|
|
// Version is the exact version string for the stored module.
|
2017-10-21 17:56:53 +02:00
|
|
|
Version string
|
|
|
|
|
|
|
|
// Dir is the directory name returned by the FileStorage. This is what
|
|
|
|
// allows us to correlate a particular module version with the location on
|
|
|
|
// disk.
|
|
|
|
Dir string
|
|
|
|
|
|
|
|
// Root is the root directory containing the module. If the module is
|
|
|
|
// unpacked from an archive, and not located in the root directory, this is
|
|
|
|
// used to direct the loader to the correct subdirectory. This is
|
|
|
|
// independent from any subdirectory in the original source string, which
|
|
|
|
// may traverse further into the module tree.
|
|
|
|
Root string
|
2017-10-26 01:20:05 +02:00
|
|
|
|
|
|
|
// url is the location of the module source
|
|
|
|
url string
|
|
|
|
|
|
|
|
// Registry is true if this module is sourced from a registry
|
|
|
|
registry bool
|
2017-10-21 17:56:53 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
// Storage implements methods to manage the storage of modules.
|
|
|
|
// This is used by Tree.Load to query registries, authenticate requests, and
|
|
|
|
// store modules locally.
|
2017-10-27 19:06:50 +02:00
|
|
|
type Storage struct {
|
2017-10-27 21:07:24 +02:00
|
|
|
// StorageDir is the full path to the directory where all modules will be
|
|
|
|
// stored.
|
2017-10-27 17:29:29 +02:00
|
|
|
StorageDir string
|
2017-10-27 21:07:24 +02:00
|
|
|
// Services is a required *disco.Disco, which may have services and
|
|
|
|
// credentials pre-loaded.
|
2017-10-27 19:06:50 +02:00
|
|
|
Services *disco.Disco
|
2017-10-27 21:07:24 +02:00
|
|
|
// Creds optionally provides credentials for communicating with service
|
|
|
|
// providers.
|
|
|
|
Creds auth.CredentialsSource
|
2017-10-27 19:06:50 +02:00
|
|
|
// Ui is an optional cli.Ui for user output
|
|
|
|
Ui cli.Ui
|
|
|
|
// Mode is the GetMode that will be used for various operations.
|
|
|
|
Mode GetMode
|
2017-10-23 17:34:50 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
func NewStorage(dir string, services *disco.Disco, creds auth.CredentialsSource) *Storage {
|
|
|
|
s := &Storage{
|
|
|
|
StorageDir: dir,
|
|
|
|
Services: services,
|
|
|
|
Creds: creds,
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure this isn't nil
|
|
|
|
if s.Services == nil {
|
|
|
|
s.Services = disco.NewDisco()
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2017-10-21 17:56:53 +02:00
|
|
|
// loadManifest returns the moduleManifest file from the parent directory.
|
2017-10-27 21:07:24 +02:00
|
|
|
func (s Storage) loadManifest() (moduleManifest, error) {
|
2017-10-21 17:56:53 +02:00
|
|
|
manifest := moduleManifest{}
|
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
manifestPath := filepath.Join(s.StorageDir, manifestName)
|
2017-10-21 17:56:53 +02:00
|
|
|
data, err := ioutil.ReadFile(manifestPath)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return manifest, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
return manifest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &manifest); err != nil {
|
|
|
|
return manifest, err
|
|
|
|
}
|
|
|
|
return manifest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the location of the module, along with the version used and the module
|
|
|
|
// root directory. The storage method loads the entire file and rewrites it
|
|
|
|
// each time. This is only done a few times during init, so efficiency is
|
|
|
|
// not a concern.
|
2017-10-27 21:07:24 +02:00
|
|
|
func (s Storage) recordModule(rec moduleRecord) error {
|
|
|
|
manifest, err := s.loadManifest()
|
2017-10-21 17:56:53 +02:00
|
|
|
if err != nil {
|
|
|
|
// if there was a problem with the file, we will attempt to write a new
|
|
|
|
// one. Any non-data related error should surface there.
|
2017-10-23 17:19:24 +02:00
|
|
|
log.Printf("[WARN] error reading module manifest: %s", err)
|
2017-10-21 17:56:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// do nothing if we already have the exact module
|
|
|
|
for i, stored := range manifest.Modules {
|
2017-10-23 17:19:24 +02:00
|
|
|
if rec == stored {
|
2017-10-21 17:56:53 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// they are not equal, but if the storage path is the same we need to
|
2017-10-23 17:19:24 +02:00
|
|
|
// remove this rec to be replaced.
|
|
|
|
if rec.Dir == stored.Dir {
|
2017-10-21 17:56:53 +02:00
|
|
|
manifest.Modules[i] = manifest.Modules[len(manifest.Modules)-1]
|
|
|
|
manifest.Modules = manifest.Modules[:len(manifest.Modules)-1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 17:19:24 +02:00
|
|
|
manifest.Modules = append(manifest.Modules, rec)
|
2017-10-21 17:56:53 +02:00
|
|
|
|
|
|
|
js, err := json.Marshal(manifest)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
manifestPath := filepath.Join(s.StorageDir, manifestName)
|
2017-10-21 17:56:53 +02:00
|
|
|
return ioutil.WriteFile(manifestPath, js, 0644)
|
|
|
|
}
|
|
|
|
|
2017-10-24 00:07:49 +02:00
|
|
|
// load the manifest from dir, and return all module versions matching the
|
|
|
|
// provided source. Records with no version info will be skipped, as they need
|
|
|
|
// to be uniquely identified by other means.
|
2017-10-27 21:07:24 +02:00
|
|
|
func (s Storage) moduleVersions(source string) ([]moduleRecord, error) {
|
|
|
|
manifest, err := s.loadManifest()
|
2017-10-24 00:07:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return manifest.Modules, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var matching []moduleRecord
|
|
|
|
|
|
|
|
for _, m := range manifest.Modules {
|
|
|
|
if m.Source == source && m.Version != "" {
|
2017-11-02 23:53:05 +01:00
|
|
|
log.Printf("[DEBUG] found local version %q for module %s", m.Version, m.Source)
|
2017-10-24 00:07:49 +02:00
|
|
|
matching = append(matching, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return matching, nil
|
|
|
|
}
|
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
func (s Storage) moduleDir(key string) (string, error) {
|
|
|
|
manifest, err := s.loadManifest()
|
2017-10-24 00:07:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range manifest.Modules {
|
|
|
|
if m.Key == key {
|
|
|
|
return m.Dir, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-10-21 17:56:53 +02:00
|
|
|
// return only the root directory of the module stored in dir.
|
2017-10-27 21:07:24 +02:00
|
|
|
func (s Storage) getModuleRoot(dir string) (string, error) {
|
|
|
|
manifest, err := s.loadManifest()
|
2017-10-21 17:56:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-10-23 17:19:24 +02:00
|
|
|
for _, mod := range manifest.Modules {
|
|
|
|
if mod.Dir == dir {
|
|
|
|
return mod.Root, nil
|
2017-10-21 17:56:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// record only the Root directory for the module stored at dir.
|
2017-10-27 21:07:24 +02:00
|
|
|
func (s Storage) recordModuleRoot(dir, root string) error {
|
2017-10-23 17:19:24 +02:00
|
|
|
rec := moduleRecord{
|
2017-10-21 17:56:53 +02:00
|
|
|
Dir: dir,
|
|
|
|
Root: root,
|
|
|
|
}
|
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
return s.recordModule(rec)
|
2017-10-21 17:56:53 +02:00
|
|
|
}
|
2017-10-23 17:58:44 +02:00
|
|
|
|
2017-11-02 23:53:05 +01:00
|
|
|
func (s Storage) output(msg string) {
|
|
|
|
if s.Ui == nil || s.Mode == GetModeNone {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.Ui.Output(msg)
|
|
|
|
}
|
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
func (s Storage) getStorage(key string, src string) (string, bool, error) {
|
2017-10-27 17:29:29 +02:00
|
|
|
storage := &getter.FolderStorage{
|
2017-10-27 21:07:24 +02:00
|
|
|
StorageDir: s.StorageDir,
|
2017-10-27 17:29:29 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 23:53:05 +01:00
|
|
|
log.Printf("[DEBUG] fetching module from %s", src)
|
2017-10-27 18:58:24 +02:00
|
|
|
|
2017-10-23 17:58:44 +02:00
|
|
|
// Get the module with the level specified if we were told to.
|
2017-10-27 21:07:24 +02:00
|
|
|
if s.Mode > GetModeNone {
|
2017-10-23 17:58:44 +02:00
|
|
|
log.Printf("[DEBUG] fetching %q with key %q", src, key)
|
2017-10-27 21:07:24 +02:00
|
|
|
if err := storage.Get(key, src, s.Mode == GetModeUpdate); err != nil {
|
2017-10-23 17:58:44 +02:00
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the directory where the module is.
|
2017-10-27 17:29:29 +02:00
|
|
|
dir, found, err := storage.Dir(key)
|
2017-10-23 17:58:44 +02:00
|
|
|
log.Printf("[DEBUG] found %q in %q: %t", src, dir, found)
|
|
|
|
return dir, found, err
|
|
|
|
}
|
2017-10-26 01:20:05 +02:00
|
|
|
|
|
|
|
// find a stored module that's not from a registry
|
2017-10-27 21:07:24 +02:00
|
|
|
func (s Storage) findModule(key string) (string, error) {
|
|
|
|
if s.Mode == GetModeUpdate {
|
2017-10-26 01:20:05 +02:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
return s.moduleDir(key)
|
2017-10-26 01:20:05 +02:00
|
|
|
}
|
|
|
|
|
2017-10-28 01:17:26 +02:00
|
|
|
// GetModule fetches a module source into the specified directory. This is used
|
|
|
|
// as a convenience function by the CLI to initialize a configuration.
|
|
|
|
func (s Storage) GetModule(dst, src string) error {
|
|
|
|
// reset this in case the caller was going to re-use it
|
|
|
|
mode := s.Mode
|
|
|
|
s.Mode = GetModeUpdate
|
|
|
|
defer func() {
|
|
|
|
s.Mode = mode
|
|
|
|
}()
|
|
|
|
|
|
|
|
rec, err := s.findRegistryModule(src, anyVersion)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
source := rec.url
|
|
|
|
if source == "" {
|
|
|
|
source, err = getter.Detect(src, pwd, getter.Detectors)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("module %s: %s", src, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if source == "" {
|
|
|
|
return fmt.Errorf("module %q not found", src)
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetCopy(dst, source)
|
|
|
|
}
|
|
|
|
|
2017-10-26 01:20:05 +02:00
|
|
|
// find a registry module
|
2017-10-27 21:07:24 +02:00
|
|
|
func (s Storage) findRegistryModule(mSource, constraint string) (moduleRecord, error) {
|
2017-10-26 01:20:05 +02:00
|
|
|
rec := moduleRecord{
|
|
|
|
Source: mSource,
|
|
|
|
}
|
|
|
|
// detect if we have a registry source
|
|
|
|
mod, err := regsrc.ParseModuleSource(mSource)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
//ok
|
|
|
|
case regsrc.ErrInvalidModuleSource:
|
|
|
|
return rec, nil
|
|
|
|
default:
|
|
|
|
return rec, err
|
|
|
|
}
|
|
|
|
rec.registry = true
|
|
|
|
|
|
|
|
log.Printf("[TRACE] %q is a registry module", mod.Module())
|
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
versions, err := s.moduleVersions(mod.String())
|
2017-10-26 01:20:05 +02:00
|
|
|
if err != nil {
|
2017-10-27 15:07:25 +02:00
|
|
|
log.Printf("[ERROR] error looking up versions for %q: %s", mod.Module(), err)
|
2017-10-26 01:20:05 +02:00
|
|
|
return rec, err
|
|
|
|
}
|
|
|
|
|
|
|
|
match, err := newestRecord(versions, constraint)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[INFO] no matching version for %q<%s>, %s", mod.Module(), constraint, err)
|
|
|
|
}
|
2017-11-02 23:53:05 +01:00
|
|
|
log.Printf("[DEBUG] matched %q version %s for %s", mod, match.Version, constraint)
|
2017-10-26 01:20:05 +02:00
|
|
|
|
|
|
|
rec.Dir = match.Dir
|
|
|
|
rec.Version = match.Version
|
|
|
|
found := rec.Dir != ""
|
|
|
|
|
|
|
|
// we need to lookup available versions
|
|
|
|
// Only on Get if it's not found, on unconditionally on Update
|
2017-10-27 21:07:24 +02:00
|
|
|
if (s.Mode == GetModeGet && !found) || (s.Mode == GetModeUpdate) {
|
|
|
|
resp, err := s.lookupModuleVersions(mod)
|
2017-10-26 01:20:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return rec, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Modules) == 0 {
|
|
|
|
return rec, fmt.Errorf("module %q not found in registry", mod.Module())
|
|
|
|
}
|
|
|
|
|
|
|
|
match, err := newestVersion(resp.Modules[0].Versions, constraint)
|
|
|
|
if err != nil {
|
|
|
|
return rec, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if match == nil {
|
|
|
|
return rec, fmt.Errorf("no versions for %q found matching %q", mod.Module(), constraint)
|
|
|
|
}
|
|
|
|
|
|
|
|
rec.Version = match.Version
|
|
|
|
|
2017-10-27 21:07:24 +02:00
|
|
|
rec.url, err = s.lookupModuleLocation(mod, rec.Version)
|
2017-10-26 01:20:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return rec, err
|
|
|
|
}
|
2017-11-02 23:53:05 +01:00
|
|
|
|
2017-11-03 15:50:19 +01:00
|
|
|
s.output(fmt.Sprintf(" Found version %s of %s on %s", rec.Version, mod.Module(), mod.RawHost.Display()))
|
2017-11-02 23:53:05 +01:00
|
|
|
|
2017-10-26 01:20:05 +02:00
|
|
|
}
|
|
|
|
return rec, nil
|
|
|
|
}
|