Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #9942: Migration script to add missing table in Postgres #1436

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
*************************************************************************************
* Copyright 2017 Normation SAS
*************************************************************************************
*
* This file is part of Rudder.
*
* Rudder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In accordance with the terms of section 7 (7. Additional Terms.) of
* the GNU General Public License version 3, the copyright holders add
* the following Additional permissions:
* Notwithstanding to the terms of section 5 (5. Conveying Modified Source
* Versions) and 6 (6. Conveying Non-Source Forms.) of the GNU General
* Public License version 3, when you create a Related Module, this
* Related Module is not considered as a part of the work and may be
* distributed under the license agreement of your choice.
* A "Related Module" means a set of sources files including their
* documentation that, without modification of the Source Code, enables
* supplementary functions or services in addition to those offered by
* the Software.
*
* Rudder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rudder. If not, see <http://www.gnu.org/licenses/>.

*
*************************************************************************************
*/

-- Create the table for the node configuration
CREATE TABLE dataSources (
id text PRIMARY KEY NOT NULL CHECK (id <> '')
, properties text NOT NULL CHECK (properties <> '' )
, status text CHECK (status <> '' )
);
1 change: 1 addition & 0 deletions rudder-core/src/main/resources/reportsSchema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ CREATE TABLE dataSources (
-- data source properties and status are valid json, until we can use postgres 9.2 keep text type (more details in configuration details)

, properties text NOT NULL CHECK (properties <> '' )
, status text CHECK (status <> '' )
);

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ trait QueryDataSourceService {
def queryOne(datasource: DataSource, nodeId: NodeId, cause: UpdateCause): Box[NodeId]
}



class HttpQueryDataSourceService(
nodeInfo : NodeInfoService
, parameterRepo : RoParameterRepository
Expand All @@ -109,26 +107,26 @@ class HttpQueryDataSourceService(

override def queryAll(datasource: DataSource, cause: UpdateCause): Box[Set[NodeId]] = {
query[Set[NodeId]]("fetch data for all node", datasource, cause
, (d:HttpDataSourceType) => queryAllByNode(datasource.name, d, cause)
, (d:HttpDataSourceType) => queryAllByNode(datasource.name, d, cause)
, (d:HttpDataSourceType) => queryAllByNode(datasource.id, d, cause)
, (d:HttpDataSourceType) => queryAllByNode(datasource.id, d, cause)
, s"All nodes data updated from data source '${datasource.name.value}' (${datasource.id.value})"
, s"Error when fetching data from data source '${datasource.name.value}' (${datasource.id.value}) for all nodes"
)
}

override def querySubset(datasource: DataSource, info: PartialNodeUpdate, cause: UpdateCause): Box[Set[NodeId]] = {
query[Set[NodeId]](s"fetch data for a set of ${info.nodes.size}", datasource, cause
, (d:HttpDataSourceType) => querySubsetByNode(datasource.name, d, info, cause)
, (d:HttpDataSourceType) => querySubsetByNode(datasource.name, d, info, cause)
, (d:HttpDataSourceType) => querySubsetByNode(datasource.id, d, info, cause)
, (d:HttpDataSourceType) => querySubsetByNode(datasource.id, d, info, cause)
, s"Requested nodes data updated from data source '${datasource.name.value}' (${datasource.id.value})"
, s"Error when fetching data from data source '${datasource.name.value}' (${datasource.id.value}) for requested nodes"
)
}

override def queryOne(datasource: DataSource, nodeId: NodeId, cause: UpdateCause): Box[NodeId] = {
query[NodeId](s"fetch data for node '${nodeId.value}'", datasource, cause
, (d:HttpDataSourceType) => queryNodeByNode(datasource.name, d, nodeId, cause)
, (d:HttpDataSourceType) => queryNodeByNode(datasource.name, d, nodeId, cause)
, (d:HttpDataSourceType) => queryNodeByNode(datasource.id, d, nodeId, cause)
, (d:HttpDataSourceType) => queryNodeByNode(datasource.id, d, nodeId, cause)
, s"Data for node '${nodeId.value}' updated from data source '${datasource.name.value}' (${datasource.id.value})"
, s"Error when fetching data from data source '${datasource.name.value}' (${datasource.id.value}) for node '${nodeId.value}'"
)
Expand Down Expand Up @@ -166,9 +164,8 @@ class HttpQueryDataSourceService(
res
}


private[this] def buildOneNodeTask(
datasourceName: DataSourceName
datasourceId: DataSourceId
, datasource : HttpDataSourceType
, nodeInfo : NodeInfo
, policyServers : Map[NodeId, NodeInfo]
Expand All @@ -182,26 +179,26 @@ class HttpQueryDataSourceService(
case Some(p) => Full(p)
})
//connection timeout: 5s ; getdata timeout: freq ?
property <- getHttp.getNode(datasourceName, datasource, nodeInfo, policyServer, parameters, datasource.requestTimeOut, datasource.requestTimeOut)
property <- getHttp.getNode(datasourceId, datasource, nodeInfo, policyServer, parameters, datasource.requestTimeOut, datasource.requestTimeOut)
newNode = nodeInfo.node.copy(properties = CompareProperties.updateProperties(nodeInfo.properties, Some(Seq(property))))
nodeUpdated <- nodeRepository.updateNode(newNode, cause.modId, cause.actor, cause.reason) ?~! s"Cannot save value for node '${nodeInfo.id.value}' for property '${property.name}'"
} yield {
nodeUpdated.id
}) match {
case Failure(msg, x, y) => Failure(s"Error when getting data from datasource '${datasourceName.value}' for node ${nodeInfo.hostname} (${nodeInfo.id.value}): ${msg}", x, y)
case Failure(msg, x, y) => Failure(s"Error when getting data from datasource '${datasourceId.value}' for node ${nodeInfo.hostname} (${nodeInfo.id.value}): ${msg}", x, y)
case x => x
}
)
}

def querySubsetByNode(datasourceName: DataSourceName, datasource: HttpDataSourceType, info: PartialNodeUpdate, cause: UpdateCause)(implicit scheduler: Scheduler): Box[Set[NodeId]] = {
def querySubsetByNode(datasourceId: DataSourceId, datasource: HttpDataSourceType, info: PartialNodeUpdate, cause: UpdateCause)(implicit scheduler: Scheduler): Box[Set[NodeId]] = {
import scala.concurrent.duration._
import net.liftweb.util.Helpers.tryo
import com.normation.utils.Control.bestEffort

def tasks(nodes: Map[NodeId, NodeInfo], policyServers: Map[NodeId, NodeInfo], parameters: Set[Parameter]): Task[List[Box[NodeId]]] = {
Task.gatherUnordered(nodes.values.map { nodeInfo =>
buildOneNodeTask(datasourceName, datasource, nodeInfo, policyServers, parameters, cause)
buildOneNodeTask(datasourceId, datasource, nodeInfo, policyServers, parameters, cause)
})
}

Expand All @@ -216,18 +213,18 @@ class HttpQueryDataSourceService(
}
}

def queryAllByNode(datasourceName: DataSourceName, datasource: HttpDataSourceType, cause: UpdateCause)(implicit scheduler: Scheduler): Box[Set[NodeId]] = {
def queryAllByNode(datasourceId: DataSourceId, datasource: HttpDataSourceType, cause: UpdateCause)(implicit scheduler: Scheduler): Box[Set[NodeId]] = {
for {
nodes <- nodeInfo.getAll()
policyServers = nodes.filter { case (_, n) => n.isPolicyServer }
parameters <- parameterRepo.getAllGlobalParameters.map( _.toSet[Parameter] )
updated <- querySubsetByNode(datasourceName, datasource, PartialNodeUpdate(nodes, policyServers, parameters), cause)
updated <- querySubsetByNode(datasourceId, datasource, PartialNodeUpdate(nodes, policyServers, parameters), cause)
} yield {
updated
}
}

def queryNodeByNode(datasourceName: DataSourceName, datasource: HttpDataSourceType, nodeId: NodeId, cause: UpdateCause)(implicit scheduler: Scheduler): Box[NodeId] = {
def queryNodeByNode(datasourceId: DataSourceId, datasource: HttpDataSourceType, nodeId: NodeId, cause: UpdateCause)(implicit scheduler: Scheduler): Box[NodeId] = {
import net.liftweb.util.Helpers.tryo
for {
allNodes <- nodeInfo.getAll()
Expand All @@ -237,7 +234,7 @@ class HttpQueryDataSourceService(
}
policyServers = allNodes.filterKeys( _ == node.policyServerId)
parameters <- parameterRepo.getAllGlobalParameters.map( _.toSet[Parameter] )
updated <- tryo(Await.result(buildOneNodeTask(datasourceName, datasource, node, policyServers, parameters, cause).runAsync, datasource.requestTimeOut))
updated <- tryo(Await.result(buildOneNodeTask(datasourceId, datasource, node, policyServers, parameters, cause).runAsync, datasource.requestTimeOut))
result <- updated
} yield {
result
Expand All @@ -254,4 +251,3 @@ class HttpQueryDataSourceService(
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class GetDataset(valueCompiler: InterpolatedValueCompiler) {

val compiler = new InterpolateNode(valueCompiler)

def getNode(datasourceName: DataSourceName, datasource: HttpDataSourceType, node: NodeInfo, policyServer: NodeInfo, parameters: Set[Parameter], connectionTimeout: Duration, readTimeOut: Duration): Box[NodeProperty] = {
def getNode(datasourceName: DataSourceId, datasource: HttpDataSourceType, node: NodeInfo, policyServer: NodeInfo, parameters: Set[Parameter], connectionTimeout: Duration, readTimeOut: Duration): Box[NodeProperty] = {
for {
p <- sequence(parameters.toSeq)(compiler.compileParameters) ?~! "Error when transforming Rudder Parameter for variable interpolation"
parameters = p.toMap
Expand Down