github.com/coreos/mantle@v0.13.0/cmd/plume/index.go (about)

     1  // Copyright 2016 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"path"
    19  
    20  	"github.com/spf13/cobra"
    21  	"golang.org/x/net/context"
    22  
    23  	"github.com/coreos/mantle/storage"
    24  	"github.com/coreos/mantle/storage/index"
    25  )
    26  
    27  var (
    28  	indexDryRun bool
    29  	cmdIndex    = &cobra.Command{
    30  		Use:   "index [options]",
    31  		Short: "Update HTML indexes for download sites.",
    32  		Run:   runIndex,
    33  		Long: `Update some or all HTML indexes for download sites.
    34  
    35  By default only a single release is updated but specifying the
    36  special board, channel, and/or version 'all' will work too.
    37  
    38  To update everything all at once:
    39  
    40      plume index --channel=all --board=all --version=all
    41      
    42  If more flexibility is required use ore index instead.`,
    43  	}
    44  )
    45  
    46  func init() {
    47  	cmdIndex.Flags().BoolVarP(&indexDryRun, "dry-run", "n", false,
    48  		"perform a trial run, do not make changes")
    49  	AddSpecFlags(cmdIndex.Flags())
    50  	root.AddCommand(cmdIndex)
    51  }
    52  
    53  func runIndex(cmd *cobra.Command, args []string) {
    54  	if len(args) > 0 {
    55  		plog.Fatal("No args accepted")
    56  	}
    57  
    58  	if specChannel == "all" {
    59  		specChannel = ""
    60  	}
    61  	if specBoard == "all" {
    62  		specBoard = ""
    63  	}
    64  	if specVersion == "all" {
    65  		specVersion = ""
    66  	}
    67  
    68  	if specChannel != "" {
    69  		if _, ok := specs[specChannel]; !ok && specChannel != "" {
    70  			plog.Fatalf("Unknown channel: %s", specChannel)
    71  		}
    72  	}
    73  
    74  	if specBoard != "" {
    75  		boardOk := false
    76  	channelLoop:
    77  		for channel, spec := range specs {
    78  			if specChannel != "" && specChannel != channel {
    79  				continue
    80  			}
    81  			for _, board := range spec.Boards {
    82  				if specBoard == board {
    83  					boardOk = true
    84  					break channelLoop
    85  				}
    86  			}
    87  		}
    88  		if !boardOk {
    89  			plog.Fatalf("Unknown board: %s", specBoard)
    90  		}
    91  	}
    92  
    93  	ctx := context.Background()
    94  	client, err := getGoogleClient()
    95  	if err != nil {
    96  		plog.Fatalf("Authentication failed: %v", err)
    97  	}
    98  
    99  	for channel, spec := range specs {
   100  		if specChannel != "" && specChannel != channel {
   101  			continue
   102  		}
   103  
   104  		for _, dSpec := range spec.Destinations {
   105  			if specVersion != "" &&
   106  				!dSpec.VersionPath &&
   107  				specVersion != dSpec.NamedPath {
   108  				continue
   109  			}
   110  
   111  			bkt, err := storage.NewBucket(client, dSpec.BaseURL)
   112  			if err != nil {
   113  				plog.Fatal(err)
   114  			}
   115  			bkt.WriteDryRun(indexDryRun)
   116  
   117  			doIndex := func(prefix string, recursive bool) {
   118  				if err := bkt.FetchPrefix(ctx, prefix, recursive); err != nil {
   119  					plog.Fatal(err)
   120  				}
   121  
   122  				job := index.NewIndexJob(bkt)
   123  				job.DirectoryHTML(dSpec.DirectoryHTML)
   124  				job.IndexHTML(dSpec.IndexHTML)
   125  				job.Recursive(recursive)
   126  				job.Prefix(prefix)
   127  				job.Delete(true)
   128  				if dSpec.Title != "" {
   129  					job.Name(dSpec.Title)
   130  				}
   131  				if err := job.Do(ctx); err != nil {
   132  					plog.Fatal(err)
   133  				}
   134  			}
   135  
   136  			if specBoard == "" && specVersion == "" {
   137  				doIndex(bkt.Prefix(), true)
   138  				continue
   139  			}
   140  
   141  			doIndex(bkt.Prefix(), false)
   142  			for _, board := range spec.Boards {
   143  				if specBoard != "" && specBoard != board {
   144  					continue
   145  				}
   146  
   147  				prefix := path.Join(bkt.Prefix(), board)
   148  				if specVersion == "" {
   149  					doIndex(prefix, true)
   150  					continue
   151  				}
   152  
   153  				doIndex(prefix, false)
   154  				doIndex(path.Join(prefix, specVersion), true)
   155  			}
   156  		}
   157  	}
   158  }