/usr/share/gocode/src/github.com/ctdk/goiardi/sandbox/sandbox.go is in golang-github-ctdk-goiardi-dev 0.11.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | /* Sandbox structs, for testing whether cookbook files need to be uploaded */
/*
* Copyright (c) 2013-2016, Jeremy Bingham (<jeremy@goiardi.gl>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package sandbox allows checking files before re-uploading them, so any given
// version of a file need only be uploaded once rather than being uploaded
// repeatedly.
package sandbox
import (
"crypto/md5"
"crypto/rand"
"database/sql"
"fmt"
"io"
"time"
"github.com/ctdk/goiardi/config"
"github.com/ctdk/goiardi/datastore"
"github.com/ctdk/goiardi/filestore"
"github.com/ctdk/goiardi/util"
"github.com/tideland/golib/logger"
)
/* The structure of the sandbox responses is... inconsistent. */
// Sandbox is a slice of checksums of files, marked completed once they've all
// been uploaded or if they've already been uploaded.
type Sandbox struct {
ID string
CreationTime time.Time
Completed bool
Checksums []string
}
/* We actually generate the sandboxID ourselves, so we don't pass that in. */
// New creates a new sandbox, given a map of null values with file checksums as
// keys.
func New(checksumHash map[string]interface{}) (*Sandbox, error) {
/* For some reason the checksums come in a JSON hash that looks like
* this:
* { "checksums": {
* "385ea5490c86570c7de71070bce9384a":null,
* "f6f73175e979bd90af6184ec277f760c":null,
* "2e03dd7e5b2e6c8eab1cf41ac61396d5":null
* } } --- per the chef server api docs. Not sure why it comes in that
* way rather than as an array, since those nulls are apparently never
* anything but nulls. */
/* First generate an id for this sandbox. Collisions are certainly
* possible, so we'll give it five tries to make a unique one before
* bailing. This may later turn out not to be the ideal sandbox creation
* method, but we'll see. */
var sandboxID string
var err error
for i := 0; i < 5; i++ {
sandboxID, err = generateSandboxID()
if err != nil {
/* Something went very wrong. */
return nil, err
}
if s, _ := Get(sandboxID); s != nil {
logger.Infof("Collision! Somehow %s already existed as a sandbox id on attempt %d. Trying again.", sandboxID, i)
sandboxID = ""
}
}
if sandboxID == "" {
err = fmt.Errorf("Somehow every attempt to create a unique sandbox id failed. Bailing.")
return nil, err
}
checksums := make([]string, len(checksumHash))
j := 0
for k := range checksumHash {
checksums[j] = k
j++
}
sbox := &Sandbox{
ID: sandboxID,
CreationTime: time.Now(),
Completed: false,
Checksums: checksums,
}
return sbox, nil
}
func generateSandboxID() (string, error) {
randnum := 20
b := make([]byte, randnum)
n, err := io.ReadFull(rand.Reader, b)
if n != len(b) || err != nil {
return "", err
}
idMD5 := md5.New()
idMD5.Write(b)
sandboxID := fmt.Sprintf("%x", idMD5.Sum(nil))
return sandboxID, nil
}
// Get a sandbox.
func Get(sandboxID string) (*Sandbox, error) {
var sandbox *Sandbox
var found bool
if config.UsingDB() {
var err error
sandbox, err = getSQL(sandboxID)
if err != nil {
if err == sql.ErrNoRows {
found = false
} else {
return nil, err
}
} else {
found = true
}
} else {
ds := datastore.New()
var s interface{}
s, found = ds.Get("sandbox", sandboxID)
if s != nil {
sandbox = s.(*Sandbox)
}
}
if !found {
err := fmt.Errorf("Sandbox %s not found", sandboxID)
return nil, err
}
return sandbox, nil
}
// Save the sandbox.
func (s *Sandbox) Save() error {
if config.Config.UseMySQL {
if err := s.saveMySQL(); err != nil {
return err
}
} else if config.Config.UsePostgreSQL {
if err := s.savePostgreSQL(); err != nil {
return err
}
} else {
ds := datastore.New()
ds.Set("sandbox", s.ID, s)
}
return nil
}
// Delete a sandbox.
func (s *Sandbox) Delete() error {
if config.UsingDB() {
if err := s.deleteSQL(); err != nil {
return nil
}
} else {
ds := datastore.New()
ds.Delete("sandbox", s.ID)
}
return nil
}
// GetList returns a list of the ids of all the sandboxes on the system.
func GetList() []string {
var sandboxList []string
if config.UsingDB() {
sandboxList = getListSQL()
} else {
ds := datastore.New()
sandboxList = ds.GetList("sandbox")
}
return sandboxList
}
// UploadChkList builds the list of file checksums and whether or not they need
// to be uploaded. If they do, the upload URL is also provided.
func (s *Sandbox) UploadChkList() map[string]map[string]interface{} {
/* Uh... */
chksumStats := make(map[string]map[string]interface{})
for _, chk := range s.Checksums {
chksumStats[chk] = make(map[string]interface{})
var n bool
if config.Config.UseS3Upload {
n = util.S3CheckFile("default", chk)
} else {
k, _ := filestore.Get(chk)
if k != nil {
n = true
}
}
if n {
chksumStats[chk]["needs_upload"] = false
} else {
// set signed s3 thingamajig here
if config.Config.UseS3Upload {
var err error
chksumStats[chk]["url"], err = util.S3PutURL("default", chk)
if err != nil {
logger.Errorf(err.Error())
}
} else {
itemURL := fmt.Sprintf("/file_store/%s", chk)
chksumStats[chk]["url"] = util.CustomURL(itemURL)
}
chksumStats[chk]["needs_upload"] = true
}
}
return chksumStats
}
// IsComplete returns true if the sandbox is complete.
func (s *Sandbox) IsComplete() error {
for _, chk := range s.Checksums {
var uploaded bool
if config.Config.UseS3Upload {
var err error
uploaded, err = util.CheckForObject("default", chk)
if err != nil {
return err
}
} else {
k, _ := filestore.Get(chk)
if k != nil {
uploaded = true
}
}
if !uploaded {
err := fmt.Errorf("Checksum %s not uploaded yet, %s not complete, cannot commit yet.", chk, s.ID)
return err
}
}
return nil
}
// GetName returns the sandbox's id.
func (s *Sandbox) GetName() string {
return s.ID
}
// URLType returns the base element of a sandbox's URL.
func (s *Sandbox) URLType() string {
return "sandboxes"
}
// AllSandboxes returns all sandboxes on the server.
func AllSandboxes() []*Sandbox {
var sandboxes []*Sandbox
if config.UsingDB() {
sandboxes = allSandboxesSQL()
} else {
sandboxList := GetList()
for _, s := range sandboxList {
sb, err := Get(s)
if err != nil {
continue
}
sandboxes = append(sandboxes, sb)
}
}
return sandboxes
}
|