MCPcopy Create free account
hub / github.com/cloudbase/garm / CreateInstance

Method CreateInstance

database/sql/instances.go:37–106  ·  view source on GitHub ↗
(ctx context.Context, poolID string, param params.CreateInstanceParams)

Source from the content-addressed store, hash-verified

35)
36
37func (s *sqlDatabase) CreateInstance(ctx context.Context, poolID string, param params.CreateInstanceParams) (instance params.Instance, err error) {
38 defer func() {
39 if err == nil {
40 s.sendNotify(common.InstanceEntityType, common.CreateOperation, instance)
41 }
42 }()
43
44 err = s.conn.Transaction(func(tx *gorm.DB) error {
45 pool, err := s.getPoolByID(tx, poolID)
46 if err != nil {
47 return fmt.Errorf("error fetching pool: %w", err)
48 }
49 var cnt int64
50 q := s.conn.Model(&Instance{}).Where("pool_id = ?", pool.ID).Count(&cnt)
51 if q.Error != nil {
52 return fmt.Errorf("error fetching instance count: %w", q.Error)
53 }
54 var maxRunners int64
55 if pool.MaxRunners > math.MaxInt64 {
56 maxRunners = math.MaxInt64
57 } else {
58 maxRunners = int64(pool.MaxRunners)
59 }
60 if cnt >= maxRunners {
61 return runnerErrors.NewConflictError("max runners reached for pool %s", pool.ID)
62 }
63
64 var labels datatypes.JSON
65 if len(param.AditionalLabels) > 0 {
66 labels, err = json.Marshal(param.AditionalLabels)
67 if err != nil {
68 return fmt.Errorf("error marshalling labels: %w", err)
69 }
70 }
71
72 var secret []byte
73 if len(param.JitConfiguration) > 0 {
74 secret, err = s.marshalAndSeal(param.JitConfiguration)
75 if err != nil {
76 return fmt.Errorf("error marshalling jit config: %w", err)
77 }
78 }
79
80 newInstance := Instance{
81 Pool: pool,
82 Name: param.Name,
83 Status: param.Status,
84 RunnerStatus: param.RunnerStatus,
85 OSType: param.OSType,
86 OSArch: param.OSArch,
87 CallbackURL: param.CallbackURL,
88 MetadataURL: param.MetadataURL,
89 GitHubRunnerGroup: param.GitHubRunnerGroup,
90 JitConfiguration: secret,
91 AditionalLabels: labels,
92 AgentID: param.AgentID,
93 Generation: param.Generation,
94 }

Callers

nothing calls this directly

Calls 5

sendNotifyMethod · 0.95
getPoolByIDMethod · 0.95
marshalAndSealMethod · 0.95
GetInstanceMethod · 0.95
MarshalMethod · 0.45

Tested by

no test coverage detected