Ejemplo n.º 1
0
func (r *Role) fillRoleFromSQL(row datastore.ResRow) error {
	var (
		rl []byte
		er []byte
		da []byte
		oa []byte
	)
	err := row.Scan(&r.Name, &r.Description, &rl, &er, &da, &oa)
	if err != nil {
		return err
	}
	r.ChefType = "role"
	r.JSONClass = "Chef::Role"
	err = datastore.DecodeBlob(rl, &r.RunList)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(er, &r.EnvRunLists)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(da, &r.Default)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(oa, &r.Override)
	if err != nil {
		return err
	}
	datastore.ChkNilArray(r)

	return nil
}
Ejemplo n.º 2
0
// Fill an environment in from a row returned from the SQL server. See the
// equivalent function in node/node.go for more details.
//
// As there, the SQL query that made the row needs to have the same number &
// order of columns as the one in Get(), even if the WHERE clause is different
// or omitted.
func (e *ChefEnvironment) fillEnvFromSQL(row datastore.ResRow) error {
	var (
		da []byte
		oa []byte
		cv []byte
	)
	err := row.Scan(&e.Name, &e.Description, &da, &oa, &cv)
	if err != nil {
		return err
	}
	e.ChefType = "environment"
	e.JSONClass = "Chef::Environment"
	err = datastore.DecodeBlob(da, &e.Default)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(oa, &e.Override)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(cv, &e.CookbookVersions)
	if err != nil {
		return err
	}
	datastore.ChkNilArray(e)
	return nil
}
Ejemplo n.º 3
0
func (s *Sandbox) fillSandboxFromPostgreSQL(row datastore.ResRow) error {
	var csb []byte
	err := row.Scan(&s.ID, &s.CreationTime, &csb, &s.Completed)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(csb, &s.Checksums)
	if err != nil {
		return err
	}
	return nil
}
Ejemplo n.º 4
0
func (r *Report) fillReportFromMySQL(row datastore.ResRow) error {
	var res, dat []byte
	var st, et mysql.NullTime
	err := row.Scan(&r.RunID, &st, &et, &r.TotalResCount, &r.Status, &r.RunList, &res, &dat, &r.NodeName)
	if err != nil {
		return err
	}
	if err = datastore.DecodeBlob(res, &r.Resources); err != nil {
		return err
	}
	if err = datastore.DecodeBlob(dat, &r.Data); err != nil {
		return err
	}
	if st.Valid {
		r.StartTime = st.Time
	}
	if et.Valid {
		r.EndTime = et.Time
	}

	return nil
}
Ejemplo n.º 5
0
// Fill in a node from a row returned from the SQL server. Useful for the case
// down the road where an array of objects is needed, but building it with
// a call to GetList(), then repeated calls to Get() sucks with a real db even
// if it's marginally acceptable in in-memory mode.
//
// NB: This does require the query to look like the one in Get().
func (n *Node) fillNodeFromSQL(row datastore.ResRow) error {
	var (
		rl []byte
		aa []byte
		na []byte
		da []byte
		oa []byte
	)
	err := row.Scan(&n.Name, &n.ChefEnvironment, &rl, &aa, &na, &da, &oa)
	if err != nil {
		return err
	}
	n.ChefType = "node"
	n.JSONClass = "Chef::Node"
	err = datastore.DecodeBlob(rl, &n.RunList)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(aa, &n.Automatic)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(na, &n.Normal)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(da, &n.Default)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(oa, &n.Override)
	if err != nil {
		return err
	}
	datastore.ChkNilArray(n)
	return nil
}
Ejemplo n.º 6
0
func (dbi *DataBagItem) fillDBItemFromMySQL(row datastore.ResRow) error {
	var rawb []byte
	err := row.Scan(&dbi.id, &dbi.dataBagID, &dbi.Name, &dbi.origName, &dbi.DataBagName, &rawb)
	if err != nil {
		return err
	}
	dbi.ChefType = "data_bag_item"
	dbi.JSONClass = "Chef::DataBagItem"
	err = datastore.DecodeBlob(rawb, &dbi.RawData)
	if err != nil {
		return err
	}
	datastore.ChkNilArray(dbi)
	return nil
}
Ejemplo n.º 7
0
func (cbv *CookbookVersion) fillCookbookVersionFromSQL(row datastore.ResRow) error {
	var (
		defb  []byte
		libb  []byte
		attb  []byte
		recb  []byte
		prob  []byte
		resb  []byte
		temb  []byte
		roob  []byte
		filb  []byte
		metb  []byte
		major int64
		minor int64
		patch int64
	)
	err := row.Scan(&cbv.id, &cbv.cookbookID, &defb, &libb, &attb, &recb, &prob, &resb, &temb, &roob, &filb, &metb, &major, &minor, &patch, &cbv.IsFrozen, &cbv.CookbookName)
	if err != nil {
		return err
	}
	/* Now... populate it. :-/ */
	// These may need to accept x.y versions with only two elements
	// instead of x.y.0 with the added default 0 patch number.
	cbv.Version = fmt.Sprintf("%d.%d.%d", major, minor, patch)
	cbv.Name = fmt.Sprintf("%s-%s", cbv.CookbookName, cbv.Version)
	cbv.ChefType = "cookbook_version"
	cbv.JSONClass = "Chef::CookbookVersion"

	/* TODO: experiment some more with getting this done with
	 * pointers. */
	err = datastore.DecodeBlob(metb, &cbv.Metadata)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(defb, &cbv.Definitions)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(libb, &cbv.Libraries)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(attb, &cbv.Attributes)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(recb, &cbv.Recipes)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(prob, &cbv.Providers)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(temb, &cbv.Templates)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(resb, &cbv.Resources)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(roob, &cbv.RootFiles)
	if err != nil {
		return err
	}
	err = datastore.DecodeBlob(filb, &cbv.Files)
	if err != nil {
		return err
	}
	datastore.ChkNilArray(cbv)

	return nil
}
Ejemplo n.º 8
0
func cookbookRecipesSQL() ([]string, util.Gerror) {
	var sqlStatement string
	if config.Config.UseMySQL {
		sqlStatement = "SELECT version, name, recipes FROM joined_cookbook_version ORDER BY name, major_ver desc, minor_ver desc, patch_ver desc"
	} else if config.Config.UsePostgreSQL {
		sqlStatement = "SELECT version, name, recipes FROM goiardi.joined_cookbook_version ORDER BY name, major_ver desc, minor_ver desc, patch_ver desc"
	}
	stmt, err := datastore.Dbh.Prepare(sqlStatement)

	if err != nil {
		log.Fatal(err)
	}
	defer stmt.Close()

	rlist := make([]string, 0)

	rows, qerr := stmt.Query()
	if qerr != nil {
		if qerr == sql.ErrNoRows {
			return rlist, nil
		}
		return nil, util.CastErr(qerr)
	}
	seen := make(map[string]bool)
	for rows.Next() {
		var n, v string
		var rec sql.RawBytes
		recipes := make([]map[string]interface{}, 0)
		err := rows.Scan(&v, &n, &rec)
		if seen[n] {
			continue
		}
		if err != nil {
			return nil, util.CastErr(err)
		}
		err = datastore.DecodeBlob(rec, &recipes)
		if err != nil {
			return nil, util.CastErr(err)
		}
		rltmp := make([]string, len(recipes))
		ci := 0
		for _, r := range recipes {
			rm := regexp.MustCompile(`(.*?)\.rb`)
			rfind := rm.FindStringSubmatch(r["name"].(string))
			if rfind == nil {
				/* unlikely */
				err := util.Errorf("No recipe name found")
				return nil, err
			}
			rbase := rfind[1]
			var rname string
			if rbase == "default" {
				rname = n
			} else {
				rname = fmt.Sprintf("%s::%s", n, rbase)
			}
			rltmp[ci] = rname
			ci++
		}
		rlist = append(rlist, rltmp...)
		seen[n] = true
	}
	rows.Close()
	if err = rows.Err(); err != nil {
		log.Fatal(err)
	}
	sort.Strings(rlist)
	return rlist, nil
}
Ejemplo n.º 9
0
func universeSQL() map[string]map[string]interface{} {
	universe := make(map[string]map[string]interface{})
	var (
		major int64
		minor int64
		patch int64
	)
	var name string

	var sqlStatement string
	if config.Config.UseMySQL {
		sqlStatement = "SELECT major_ver, minor_ver, patch_ver, c.name, metadata FROM cookbook_versions cv LEFT JOIN cookbooks c ON cv.cookbook_id = c.id ORDER BY cv.cookbook_id, major_ver DESC, minor_ver DESC, patch_ver DESC"
	} else if config.Config.UsePostgreSQL {
		sqlStatement = "SELECT major_ver, minor_ver, patch_ver, c.name, metadata->>'dependencies' FROM goiardi.cookbook_versions cv LEFT JOIN goiardi.cookbooks c ON cv.cookbook_id = c.id ORDER BY cv.cookbook_id, major_ver DESC, minor_ver DESC, patch_ver DESC"
	}
	stmt, err := datastore.Dbh.Prepare(sqlStatement)

	if err != nil {
		log.Fatal(err)
	}
	defer stmt.Close()

	rows, qerr := stmt.Query()
	if qerr != nil {
		if qerr == sql.ErrNoRows {
			return universe
		}
		log.Fatal(qerr)
	}

	for rows.Next() {
		var metb sql.RawBytes
		metadata := make(map[string]interface{})
		u := make(map[string]interface{})
		err := rows.Scan(&major, &minor, &patch, &name, &metb)
		if err != nil {
			log.Fatal(err)
		}
		err = datastore.DecodeBlob(metb, &metadata)
		if err != nil {
			log.Fatal(err)
		}
		version := fmt.Sprintf("%d.%d.%d", major, minor, patch)
		customURL := fmt.Sprintf("/cookbook/%s/%s", name, version)
		u["location_path"] = util.CustomURL(customURL)
		u["location_type"] = "chef_server"

		if config.Config.UsePostgreSQL {
			u["dependencies"] = metadata
		} else {
			u["dependencies"] = metadata["dependencies"]
		}
		if _, ok := universe[name]; !ok {
			universe[name] = make(map[string]interface{})
		}
		universe[name][version] = u
	}
	rows.Close()
	if err = rows.Err(); err != nil {
		log.Fatal(err)
	}
	return universe
}